Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pinsrw

macrumors regular
Original poster
May 30, 2010
194
0
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.
 
Last edited by a moderator:
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].
 
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.
 
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.
 
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.

What if total is less than 5? I'm pretty sure scrolling to a negative index is not possible
 
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.