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

Jordan72

macrumors member
Original poster
Nov 23, 2005
88
0
Some objects in Cocoa you have to add to autorelease pools and others are added by a Foundation method. Take for instance, these NSNumber messages:

I don't have to release the instance pointed to by myInt, when myInt is assigned to point the an instance the following way:

Code:
id myInt = [NSNumber numberWithInt:100];

Do I have to release an NSNumber instance if I allocate it this way, since I do use alloc?:

Code:
id myInt = [[NSNumber alloc] initWithInt:100];

On page 318, second paragraph of Kochan's book Programming in Objective-C it states:

In general, you don't need to worry about releasing an object returned by a Foundation method. Sometimes the object is owned by the method that returns it. Other times, the object is newly created and added to the autorelease pool by the method.

What does this mean that it is owned by the method that returns it? And, how is it added to the autorelease pool without an explicit message which would do so?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Basic Cocoa Convention:

Anything you alloc, init has a retain count of 1 and needs to be released or autoreleased. Any object you get in another way should be autoreleased by whatever code passes it to you.

It's normally worth sticking to this in your own code to avoid confusion.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
robbieduncan said:
Basic Cocoa Convention:

Anything you alloc, init has a retain count of 1 and needs to be released or autoreleased. Any object you get in another way should be autoreleased by whatever code passes it to you.

It's normally worth sticking to this in your own code to avoid confusion.
And also anything you create using 'copy' or 'mutableCopy' needs to be released. Otherwise you can assume they're someone else's problem
What does this mean that it is owned by the method that returns it? And, how is it added to the autorelease pool without an explicit message which would do so?
It's added by the autorelease pool immediately before the foundation method returns.
Code:
[returnValue autorelease];
[return returnValue];
The foundation classes may actually do this in C but the principle is the same.

When you autorelease something it doesn't immediately disappear like it would with release. It is in a bunch of objects that will be released soon. This is why you can happily use a autoreleased object in your code but only if you won't need it for long. If there's any chance something you do will take a bit of time to do (like network or i/o access) you should really retain it in case the object is autoreleased whilst you still need it.
 

Jordan72

macrumors member
Original poster
Nov 23, 2005
88
0
robbieduncan said:
Anything you alloc, init has a retain count of 1 and needs to be released or autoreleased. Any object you get in another way should be autoreleased by whatever code passes it to you.

So, if I allocate a NSString and NSNumber in the following ways, is it true that they are not added to the autorelase pool?

Code:
NSNumber * myNum = [[NSNumber alloc] initWithInt:100];

NSString * myStr = [[NSString alloc] initWithString:@"A string."];

If they are added, then I don't need to release them. If not, then I must send them a release meassage to manage my programs memory competently.
 

Jordan72

macrumors member
Original poster
Nov 23, 2005
88
0
Thanks for the help. I think I'm all cleared up on that now. I'm sort of paranoid about having the memory thing down from the get go, because I don't want the headaches later.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.