I am devoloping an app on iPad(iPhone), which needs to use NSMutalbleURLRequest. Instrument told me there is a memeroy leak in the marked line below:
-(NSMutableURLRequest*) createRequest:(NSString *)strURL withData:(NSData *)strBody withVersion: (int) version
{
NSURL* url = [NSURL URLWithString: strURL]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url]; (LEAKED HERE) [request setHTTPMethod: @"POST"]; [request setHTTPBody: strBody];
NSString* strVersion = [NSString stringWithFormat: @"%d", version]; [request addValue: strVersion forHTTPHeaderField: @"v"]; [request addValue: userAgent forHTTPHeaderField: @"User-Agent"]; return request; }
I thought it may be an apple bug of "[NSMutableURLRequest requestWithURL: url]" cause the leak. so I tried to add some lines before that line, like this: // added lines
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init]; [request release];
Amazingly, these added lines causes leak now and the "[NSMutableURLRequest requestWithURL: url]" doesn't leak any more!
Because the methode "-(NSMutableURLRequest*) createRequest ..." I wrote is running in a "while (YES)"block in a thread, I think, maybe I can put the added lines in the init method of the class, so it maybe only leak once and wont cause mass memory leak.
IT DID WORKS!
I am a new developer in iOS platform, I have benifit a lot from here, so, I post my experence here, waiting for confirm(if I am not right about above), or posting for feedback to the gread stackoverflow.com
Another thing:
[[NSDate alloc] initWithString: [n.content stringByAppendingString: @" +0800"]];
this line also causes memory leak.
and I avoid it by using NSDataFormatter and NSLocale. I think it's an nother apple bug.
another and: to avoid the great NSXMLPareser memory leak, my partener write a lite one by himself...
ps:This is my fist time posing here, has't figure out how to format the code with the "101010" button, i am sorry...