1
\$\begingroup\$

Just need to know if I'm doing something wrong in this code. my app seem work fast now with this code. I just want to if i really understant that

   - (void)receive
{


    NSString *post2 = [NSString stringWithFormat:@"expediteur=%@&destinataire=%@",
                       [[expediteurLbl text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                       [[destinataireLbl text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSData *dataToSend2 = [NSData dataWithBytes:[post2 UTF8String] length:[post2 length]  ];

    request2 = [[[NSMutableURLRequest alloc] init] autorelease];
    [request2 setURL:[NSURL URLWithString:@"http:/****************.php"]];
    [request2 setHTTPMethod:@"POST"];
    [request2 setHTTPBody:dataToSend2];
    [request2 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];

    [NSThread detachNewThreadSelector:@selector(displayView) toTarget:self withObject:nil]; 

    }

    -(void)displayView

    {
        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        NSURLResponse *response2;
        NSError *error2;
        NSData *data2 = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&response2 error:&error2];
        reponseServeur2= [[NSMutableString alloc] initWithData:data2 encoding: NSASCIIStringEncoding];



        responseString2 = [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];

        [[reponseServeur2 stringByReplacingOccurrencesOfString:@"\n"  withString:@""] mutableCopy];


        self.messageArray = [responseString2 JSONValue];
        [messTableView reloadData];




        [pool release];


    }

and

- (void)viewDidLoad {
    [super viewDidLoad];

    [self performSelectorInBackground:@selector(receive) withObject:nil];
}
\$\endgroup\$

migrated from stackoverflow.com Sep 17 '11 at 1:26

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ i don't have errors but in console somes warnings "NSAutoreleaseNoPool(): Object 0x4e45ab0 of class NSCFString autoreleased with no pool in place - just leaking". \$\endgroup\$ – user627441 Sep 17 '11 at 0:16
  • \$\begingroup\$ You're not autoreleasing any objects inside the autorelease pool, so why have you used it inside displayView? \$\endgroup\$ – Alex Sep 17 '11 at 0:23
  • \$\begingroup\$ i can not release NSArray? \$\endgroup\$ – user627441 Sep 17 '11 at 0:27
  • \$\begingroup\$ What array? The only array there might be is self.messageArray. Looks like you've synthesized that property, so you'll only need to release it in your dealloc method if you're using retain or copy on the property. \$\endgroup\$ – Alex Sep 17 '11 at 0:30
  • \$\begingroup\$ ok i understand. but I 'trying to do a thread not to slow down the application \$\endgroup\$ – user627441 Sep 17 '11 at 0:34
2
\$\begingroup\$

I gather that request2 is an instance variable? It's risky at best to set it with an autoreleased value and then expect it to be valid later in your displayView method.

But it probably works because you execute receive in a background task, and background tasks have no default autorelease pool. If it weren't for that request2 would likely go "poof" before it got to displayView.

So all of the autoreleased data items in receive -- post2, dataToSend2, request2, several temporary strings, and at least one temporary URL -- are leaking.

\$\endgroup\$
1
\$\begingroup\$

Just need to know if I'm doing something wrong in this code.

The short answer is YES.

  1. request2 is allocated and autoreleased in a different thread than its referenced and may be dealloc'd before you get to use it in displayView. This would be a 'race condition' and you probably won't see it happen consistently, but it'll crash the app when it does happen.
  2. reponseServeur2 and responseString2 are never released (or autoreleased) at all. These are memory leaks
  3. The line [[reponseServeur2 stringByReplacingOccurrencesOfString:@"\n" withString:@""] mutableCopy]; does nothing. You'd want to assign the result of that expression to some variable.
  4. You appear to repeat the sequence:

    self.messageArray = [responseString2 JSONValue];
    [messTableView reloadData];
    

    which is probably just a typo.

  5. Everything in receive is happening without an Autorelease pool, so post2, dataToSend2, and request2 will leak. The warning you report is for post2, probably. Minimally, you need to wrap the contents of receive in an NSAutoreleasePool just like displayView.
\$\endgroup\$
  • \$\begingroup\$ ok thk for answer but i m sorry i m dropped :( . i try to something to use receveive in background for not slowing the app \$\endgroup\$ – user627441 Sep 17 '11 at 0:56
  • \$\begingroup\$ 6. You shouldn't be updating the view from your thread; the UIKit is generally main-thread-only unless explicitly documented otherwise. \$\endgroup\$ – bbum Nov 29 '11 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy