RE: [Newbie Q] Memory Management in Cocoa
RE: [Newbie Q] Memory Management in Cocoa
- Subject: RE: [Newbie Q] Memory Management in Cocoa
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Tue, 5 Nov 2002 14:32:42 -0500
>
Hi All,
>
>
I'm getting a little bit confused about when to dealloc an instance.
>
It's obvious that instances created like [[foo alloc] init ]; should be
>
deallocated when done with.
>
But what about instances created like:
>
NSString * aNSString = [NSString stringWithCString:"Hello" length:5];
>
Never call dealloc yourself. I think you meant release.
You should not release the string in your example. Class methods (e.g.,
most of the "With" methods for NSString and NSArray) return autoreleased
instances. Your string will be released at the end of the code block.
Usually this means the retainCount drops to zero and the object is dealloced
automagically. If you don't want that to happen, retain the object.
Instances you create with init or copy stick around until explicitly
released (or autoreleased and the pool gets released) and their retainCount
goes to zero.
Check out the memory management articles at stepwise.com.
Jonathan
_______________________________________________
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.