Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 24, 2005, 06:33 PM   #1
Jordan72
macrumors member
 
Join Date: Nov 2005
Special Memory Management Foundation Types

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:

Quote:
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?
Jordan72 is offline   0 Reply With Quote
Old Nov 25, 2005, 02:43 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Nov 25, 2005, 03:05 AM   #3
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Quote:
Originally Posted by robbieduncan
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
Quote:
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.

Last edited by caveman_uk; Nov 25, 2005 at 03:16 AM.
caveman_uk is offline   0 Reply With Quote
Old Nov 25, 2005, 03:59 PM   #4
Jordan72
Thread Starter
macrumors member
 
Join Date: Nov 2005
Quote:
Originally Posted by robbieduncan
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 is offline   0 Reply With Quote
Old Nov 25, 2005, 04:14 PM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Those objects have a retain count of 1 and are not in any autorelease pool so need releasing at some point.
robbieduncan is offline   0 Reply With Quote
Old Nov 25, 2005, 04:44 PM   #6
Jordan72
Thread Starter
macrumors member
 
Join Date: Nov 2005
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.
Jordan72 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Optimizing memory management activate OS X 10.8 Mountain Lion 15 Jun 23, 2013 08:07 PM
Memory management is confusing! nashyo iPhone/iPad Programming 1 Mar 2, 2013 07:33 PM
Improved memory management brijazz OS X 10.8 Mountain Lion 18 Jul 28, 2012 12:17 PM
Memory Management StuffMattSays Mac OS X 10.7 Lion 12 Jul 3, 2012 02:48 PM
iPad: Browser with better memory management porcupine8 Jailbreaks and iOS Hacks 5 Jun 6, 2012 02:45 AM

Forum Jump

All times are GMT -5. The time now is 04:51 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC