How is this a Leak?

Discussion in 'iOS Programming' started by Duke Leto, Feb 1, 2009.

  1. Duke Leto macrumors regular

    Joined:
    Mar 17, 2008
    #1
    I have some code that is giving me trouble.

    Apparently Instruments tells me that there is a leak in the string:
    PHP:
    aNum round([aSlider value]);
    NSString *aString = [[NSString allocinitWithFormat:@"%1.2f"round(aNum)];
    [
    aText setText:aString];
    [
    aString release];
    (aNum, aSlider and aText are properties of the class)
    What I do not get is how aString is leaking!
     
  2. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #2
    I don't see it either. Can you post the output from Instruments?
     
  3. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #3
    It is a NCSFString, it is 16 bytes, and it has trouble with the line:
    PHP:
    [aText setText:[NSString stringWithFormat:@"%1.2f"aNum]];
    Beats me!
     
  4. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #4
    Admittedly I've only still working my way through Objective-C learning (100 more pages!) so I might not have the best insight but...

    Are you releasing aText during your dealloc?

    Actually I guess you might need to release it in places other than the dealloc... but yeah maybe check that?
     
  5. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #5
    Yep, in all my classes, all my retained properties are released in the dealloc.
     
  6. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #6
    Just don't run Instruments anymore... pretend it isn't happening!
     
  7. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
  8. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #8
    Code:
    aNum = [B]round[/B]([aSlider value]);
    NSString *aString = [[NSString alloc] initWithFormat:@"%1.2f", [B]round[/B](aNum)];
    try fixing this first. you may not need round at all, since the printf format will round for you.
     
  9. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #9
    Got that leak .. but found another interesting one in a drawRect function:

    PHP:
    CGContextRef context UIGraphicsGetCurrentContext();
    CGContextClearRect(contextrect);
    [
    fillColor setFill];
    CGContextFillEllipseInRect(contextrect);
     
  10. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
  11. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #11
    By changing it to:
    PHP:
    aNum = [aSlider value];
    aNum round(aNum);
    and by getting rid of the second round() call.
     
  12. Duke Leto thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #12
    By the way, is it possible to use Instruments to analyze how the program runs on the phone? There seems to be a problem with the phone because it doesn't have the processing power, and I need Instruments to be able to determine what the process is and how I can stop it.
     

Share This Page