I am new to iPhone Development . and my question is on memory management
Here is my code :
- (void)asynchronousRequestServerWithXMLPost:(NSString *)urlAddress PostData:(NSString *)postContent {
urlAddress = [self encodeStringForURL:urlAddress];
theRequest = [[[NSMutableURLRequest alloc] init]autorelease];
[theRequest setURL:[NSURL URLWithString:urlAddress]];
if([postContent length] > 0) {
[theRequest setHTTPMethod:@"POST"];
} else {
[theRequest setHTTPMethod:@"GET"];
}
NSData *theBodyData = [postContent dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[theRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *postLength = [NSString stringWithFormat:@"%d", [theBodyData length]];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:theBodyData];
[NSThread detachNewThreadSelector:@selector(sendSyncRequest) toTarget:self withObject:nil];
}
I am getting Leaks on these lines :
theRequest = [[[NSMutableURLRequest alloc] init]autorelease];
[theRequest setURL:[NSURL URLWithString:urlAddress]];
NSData *theBodyData = [postContent dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[theRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
If anyone can help then help me.
Thanking You.
I am guessing your leak is being created by this line:
I would need to see your code for encodeStringForURL, but my guess if you are passing it back without it autoreleasing. Also, I wouldn’t assign the result of that method to your parameter variable, which you will then lose the reference to. You should do something like this instead:
Then use encodedUrlAddress when you call URLWithString.