Freeing up objects

Discussion in 'iOS Programming' started by pinsrw, Mar 24, 2011.

  1. pinsrw, Mar 24, 2011
    Last edited by a moderator: Mar 24, 2011

    pinsrw macrumors regular

    Joined:
    May 30, 2010
    #1
    Hi all,

    Can someone clarify what the deal is with freeing up objects. I am wondering, is this valid:

    Code:
    NSString *foo = [[NSString alloc] initWithCString: myStr];
    [my_label setText: foo];
    [foo release];
    This type of usage seems to cause an error. But I'm wondering, what about this?

    Code:
    NSString *foo = [[NSString alloc] initWithCString: myStr];
    NSMutableString *moo = [[NSMutableString alloc] initWithString: @"moo"];
    [moo appendString: foo];
    [foo release];
    When I've searched for tutorials on this I don't find much via google except spam pages.

    Thanks.
     
  2. Menge macrumors 6502a

    Menge

    Joined:
    Dec 22, 2008
    Location:
    Amsterdam
    #2
    The first usage is perfectly valid because the convention is that setText will either [copy] or [retain] the object. It should only throw an error if "myStr" is invalid.

    The second usage has a memory leak in that it leaks "moo".

    In Objective-C, if you allocate it via [[Class alloc] init(...)], then you must either [release] or [autorelease].
     
  3. pinsrw thread starter macrumors regular

    Joined:
    May 30, 2010
    #3
    As opposed to using [Class new] ?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
  5. pinsrw thread starter macrumors regular

    Joined:
    May 30, 2010
    #5
    Hi again folks,

    Something that makes little sense. If I try to free up the NSIndexPath below, my program crashes. Is there some special rule about using this class with UITableView?
    Code:
    
    int total = myFunc();
    NSIndexPath *ip = [NSIndexPath indexPathForRow: total-5
                        inSection: 0];
    [table scrollToRowAtIndexPath: ip
           atScrollPosition: UITableViewScrollPositionTop
            animated: NO];
    Thanks.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Reread the Memory Management Guide (see above post), especially the Rules section. Start with the fundamental rule, and ask yourself this question: "Do I own the NSIndexPath when I'm telling it to release?". If you don't know what ownership is, you haven't read the guide.
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    What if total is less than 5? I'm pretty sure scrolling to a negative index is not possible
     
  8. pinsrw thread starter macrumors regular

    Joined:
    May 30, 2010
    #8
    Yes, I checked for that earlier. Thanks for your concern.
     
  9. hayesk macrumors 65816

    Joined:
    May 20, 2003
    #9
    Basically,
    Code:
    Classname *myThing = [Classname thingFromParameter:myParameter];
    is autoreleased already. You don't need to free it because you don't own it.

    If you had
    Code:
    Classname *myThing = [[Classname alloc] initWithMyParameter:myParameter]
    then it would be retained since you allocated it yourself.
     

Share This Page