memory management
memory management
- Subject: memory management
- From: Daniel Richman <email@hidden>
- Date: Sat, 26 Jul 2008 16:44:28 -0700
Hi All,
I just finished reading the docs on memory management and want to make
sure I got everything right (for non-GC apps):
1) Any object returned by new, alloc, copy or any derivates
(mutableCopy, etc.) is 'yours.' You must release/autorelease it yourself.
2) Objects not returned by any of the above methods were previously sent
autorelease by their class. Since autorelease will release the objects
after the event loop is finished,
a) you may use the object as a temporary thing for the remainder of the
event loop, including returning it to calling methods
b) if you want to use the object sometime after the current event loop,
you should either i) copy it (which makes you responsible for the new
object, which has a retain count of 1, or ii) send it retain. In both
cases, you are responsible for properly releasing/autoreleasing the
objects however many times you retained them. If you used method i), you
must also remember to send it release/autorelease again since it came to
you with a retain count of 1.
Now let us suppose that I have a class Foo with instance variables V1
and V2 (ints). If I were to create a class method:
+ (Foo *)fooWithV1:(int)a V2:(int)b {
Foo *newFoo = [[Foo alloc] init];
[newFoo setV1:a];
[newFoo setV2:b];
return newFoo;
}
then I should also put, just before [return newFoo];, [newFoo autorelease];.
I know this is convention. Should I not try to meddle with this, or may
I not send the message [newFoo autorelease] if I note this clearly in
the docs (this is a quite simple class that just stores two variables;
it's intended for use only by the app it's in)?
Thanks,
Daniel
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden