Re: Memory Mysteries
Re: Memory Mysteries
- Subject: Re: Memory Mysteries
- From: publiclook <email@hidden>
- Date: Fri, 25 Jul 2003 21:33:35 -0400
On Friday, July 25, 2003, at 07:54 PM, email@hidden wrote:
Hello,
I'm a little confused about managing memory in cocoa. I know I should
release objects that have been allocated like this:
NSObject *object = [[NSObject alloc] init];
Yes
But do you release things like this?
NSFileManager *fm = [NSFileManager defaultManager];
No
Or things like this:
NSString *string = [@"~/Desktop/" stringByExpandingTildeInPath];
No
See Apple's Cocoa documentation or any of the excellent articles on the
subject:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
http://www.stepwise.com/Articles/Technical/MemoryManagement.html
http://www.stepwise.com/Articles/Technical/HoldMe.html
The simple expression of Cocoa's rules follows:
1) If you send a message that includes the word "alloc" or you send a
message that includes the word "copy" or you send the -retain message
then you should send -release or -autorelease to the same object to
which you sent the alloc, copy, or retain message.
2) If you did not send a message that includes the word "alloc" and you
did not send a message that includes the word "copy" and you did not
send the -retain message, then you should not send -release or
-autorelease to the same object.
The next line includes the word alloc:
NSObject *object = [[NSObject alloc] init];
So it is your responsibility to eventually release object.
NSFileManager *fm = [NSFileManager defaultManager];
The above line does not include an alloc, copy, or retain message so
you should not relaes or autorelease fm.
NOTE: Because you did not allocate fm, it will be automatically
dealocated at some time in the future. If you need to use fm beyond
the scope in which you received fm, you need to retain fm. If you
retain fm, then you must eventually release or autorelease fm.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.