PDA

View Full Version : Memory Leak, how to resolve. Please Guide.




oceandrive
Jan 2, 2009, 09:09 AM
Hello,

Here is my sample code where I am having problem.


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"in here--------");
CLLocationCoordinate2D loc = [newLocation coordinate];
NSMutableString *logString = [[NSMutableString alloc] initWithString: @"MyData="];
[logString appendString: myText.text];
[logString appendString: @"&MyLat="];
[logString appendString: [NSString stringWithFormat: @"%f", loc.latitude]];
[logString appendString: @"&MyLong="];
[logString appendFormat: [NSString stringWithFormat: @"%f", loc.longitude]];
NSLog(logString);
[SubmitData submitMyDetails: logString];
//[logString release];
}



I need to submit by lat long to the the server which I am doing it as above. But I am getting a Memory leak with the NSMutableString. When I tried to release the string after I did the submit my submitDetails method is not able to access the logString .

Can some one guide me on how to resolve this issue.

Thanks for the help



xsmasher
Jan 2, 2009, 12:44 PM
You should uncomment that "[logString release];" someone has to release that object when its job is done. Doing a release should not affect a method that has already been called.

Does "submitMyDetails" use the string, or does it store it away in a variable to be used later? If it stores it away, it should retain the variable and release it when its finished. Retaining a variable is how you make sure it doesn't disappear our form under you; release is how you indicate that you're done with it. When the number of releases = the number of inita and retains, the object goes away.

Can you post the code for "submitMyDetails" ?

oceandrive
Jan 2, 2009, 03:35 PM
Thanks for the help.

You are right , I was not doing the retain and release in my submitData method. Added those and looks like its working perfect.

No more memory leaks.

Should I also worry about anything else apart from memory leaks ?

xsmasher
Jan 2, 2009, 10:49 PM
Thanks for the help.

You are right , I was not doing the retain and release in my submitData method. Added those and looks like its working perfect.

No more memory leaks.

Should I also worry about anything else apart from memory leaks ?

Excellent!

Keep an eye on your total memory too - that's the top graph in instruments, next to "object allocations." If you click on the graph it'll give you the amount of memory you're using. Some people fix leaks by retaining *everything* for *forever* - so their memory climbs and climbs. :)

It should go up when your app starts, and maybe rise more as you open views / start processes, but it should plateau at some point, and perhaps go down if you're closing views and deleting objects.