Re: [Newbie Q] Memory Management in Cocoa
Re: [Newbie Q] Memory Management in Cocoa
- Subject: Re: [Newbie Q] Memory Management in Cocoa
- From: "Clark S. Cox III" <email@hidden>
- Date: Tue, 5 Nov 2002 13:50:18 -0500
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Tuesday, Nov 5, 2002, at 13:22 US/Eastern, Flemming Bengtsson wrote:
Hi All,
I'm getting a little bit confused about when to dealloc an instance.
Never. You should never call dealloc directly (unless you're doing
something out of the ordinary)
It's obvious that instances created like [[foo alloc] init ]; should be
deallocated when done with.
No, they should be released or autoreleased when you done with them.
If you are the only one who "owns" that object, then it will dealloc
itself when you release it, otherwise, it will stick around until it's
other "owners" release it as well.
But what about instances created like:
NSString * aNSString = [NSString stringWithCString:"Hello" length:5];
Those are autoreleased. i.e. they are added to a list of objects to be
automatically released at some point in the future (usually the next
pass through the event loop). The basic rule is that you should only
releas objects that you "own. You own an object if:
You allocated it yourself (i.e. you called [SomeClass alloc],
[SomeClass allocWithZone:], or [SomeClass new])
You copied it yourself (i.e. you called [someInstance copy] or
[someInstance copyWithZone:])
You explicitly retained it (i.e. you called [someInstance retain])
Generally, you should assume that any other objects are owned by
someone else. When you want to relinquish ownership of an object you do
that by calling [someInstance release] or [someInstance autorelease]
- --
http://homepage.mac.com/clarkcox3/
email@hidden
Clark S. Cox, III
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (Darwin)
iEYEARECAAYFAj3IEvAACgkQd6STocYT1xX2SQCeIdSy6RTKuouWZYEiF+eIHzuD
VNkAnRfPQ7foZHYWXq1HSMmvRXXhLOOt
=YkH2
-----END PGP SIGNATURE-----
_______________________________________________
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.