Re: Is an just allocated object allways set to autorelease? What about my subclasses?
Re: Is an just allocated object allways set to autorelease? What about my subclasses?
- Subject: Re: Is an just allocated object allways set to autorelease? What about my subclasses?
- From: Ondra Cada <email@hidden>
- Date: Fri, 21 Dec 2001 14:53:52 +0100
Alexandre,
>
>>>>> Alexandre Aybes (AA) wrote at Fri, 21 Dec 2001 12:31:00 +0100:
AA> fairly new to Cocoa/ObjC and I would like to know if all the
AA> basic Cocoa objects (the ones from the AppKit) are set to
AA> autorelease when I call "[[XXX alloc] init]".
Nope, in this case they never are.
The convention is:
- if you make the object using alloc, copy, mutableCopy, or (deprecated)
new, you have to release it yourself;
- if you obtain an object by other way -- especially by any of the class
methods like stringWith... or arrayWith... or so -- you don't have to release
it (ie. it is either shared, or autoreleased).
AA> And another question when I create my own class, should I do:
AA>
AA> - (id)init {
AA> if (self = [super init]) {
AA> // do my initialization
AA> }
AA> return self;
AA> }
This is the correct pattern. In case you want to provide class methods for
creating objects too, they should look like this:
+(Foobar*)foobar {
return [[[self alloc] init] autorelease];
}
AA> Also if the fact that they are autoreleased depends on the
AA> object, where can I find the docs for that?
It does not depend.
Theoretically, classes which represent static objects (which are never
deallocated anyway) might re-implement -autorelease to be an empty operation
to gain some efficiency, but I doubt is would be worth the effort, and I
doubt it is actually used anywhere.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc