0

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.

HML
  • 103
  • 1
  • 9
  • Why don't you use ARC?? It will solve all your problems – Garoal May 22 '12 at 05:25
  • why you are retaining after release - [theRequest retain]; ?? – rishi May 22 '12 at 05:30
  • i am just working on memory leak.may be it does not make any sense to retain that obj.so let me remove that line. – HML May 22 '12 at 05:33
  • Did you get any details with your leak? Something about autorelease pools? – Ja͢ck May 22 '12 at 05:49
  • no and its not a big leak but when ever you call leak is arising. – HML May 22 '12 at 05:51
  • 1
    @user687732: You can anytime enable ARC (per file basis). Just check this out -- http://stackoverflow.com/questions/10523816/how-to-enable-arc-for-a-single-file – viral May 22 '12 at 06:32

1 Answers1

0

I am guessing your leak is being created by this line:

urlAddress = [self encodeStringForURL:urlAddress];

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:

NSString *encodedUrlAddress = [self encodeStringForURL:urlAddress];

Then use encodedUrlAddress when you call URLWithString.

Joel
  • 15,654
  • 5
  • 37
  • 60
  • here it is: - (NSString *)encodeStringForURL:(NSString *)originalURL { NSMutableString *escaped = [[[NSMutableString alloc] init]autorelease]; [escaped appendString:[originalURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; return escaped; } – HML May 22 '12 at 06:14
  • Why isn't it just: return [[[originalURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] retain] autorelease]; – Joel May 22 '12 at 06:24
  • Make the change I suggested in my post, and the change in the comment above to encodeStringForURL and see if it is still leaking. – Joel May 22 '12 at 06:26
  • hi I have custom tabbar.In tabbar, when i am going back i am calling one method using NSNotificationCenter.In that method i am just releasing all the objects.but when i checked it in instrument i found that allocated objects are not going to dealloc.Code is here:-(void)backBtnPessed { [[NSNotificationCenter defaultCenter] postNotificationName:@"GeneralViewController" object:nil]; [self.navigationController popViewControllerAnimated:YES]; } please help me.Thank you... – HML May 22 '12 at 11:52
  • Post a new question. This is not a valid comment. – Joel May 22 '12 at 14:14