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];
}
2 Answers 2
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.
Just need to know if I'm doing something wrong in this code.
The short answer is YES.
- 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.
reponseServeur2
andresponseString2
are never released (or autoreleased) at all. These are memory leaks- The line
[[reponseServeur2 stringByReplacingOccurrencesOfString:@"\n" withString:@""] mutableCopy];
does nothing. You'd want to assign the result of that expression to some variable. You appear to repeat the sequence:
self.messageArray = [responseString2 JSONValue]; [messTableView reloadData];
which is probably just a typo.
- Everything in
receive
is happening without an Autorelease pool, sopost2
,dataToSend2
, andrequest2
will leak. The warning you report is forpost2
, probably. Minimally, you need to wrap the contents ofreceive
in an NSAutoreleasePool just likedisplayView
.
-
\$\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– user6274412011年09月17日 00:56:13 +00:00Commented Sep 17, 2011 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– bbum2011年11月29日 22:00:07 +00:00Commented Nov 29, 2011 at 22:00
displayView
? \$\endgroup\$self.messageArray
. Looks like you've synthesized that property, so you'll only need to release it in yourdealloc
method if you're usingretain
orcopy
on the property. \$\endgroup\$