Memory Leak, how to resolve. Please Guide.

Discussion in 'iOS Programming' started by oceandrive, Jan 2, 2009.

  1. macrumors newbie

    Joined:
    Nov 17, 2008
    #1
    Hello,

    Here is my sample code where I am having problem.

    PHP:
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 

        
    NSLog(@"in here--------");    
        
    CLLocationCoordinate2D loc = [newLocation coordinate];
        
    NSMutableString *logString = [[NSMutableString allocinitWithString: @"MyData="];
        [
    logString appendStringmyText.text];
        [
    logString appendString: @"&MyLat="];
        [
    logString appendString: [NSString stringWithFormat: @"%f"loc.latitude]];
        [
    logString appendString: @"&MyLong="];
        [
    logString appendFormat: [NSString stringWithFormat: @"%f"loc.longitude]];
        
    NSLog(logString);
        [
    SubmitData submitMyDetailslogString];
        
    //[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
     
  2. macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #2
    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" ?
     
  3. thread starter macrumors newbie

    Joined:
    Nov 17, 2008
    #3
    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 ?
     
  4. macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #4
    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.
     

Share This Page