Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alecbenzer

macrumors newbie
Original poster
May 2, 2010
7
0
is there an destructor equivalent to the +(void) initialize static constructor of NSObject? If not, where should things that are alloc'd in a static constructor be released? I don't see anything like +(void) dealloc in the NSObject doc.
 
Usually objects created with a class method (static method) are returned. Thus you are responsible for releasing the object if its not being auto-released.

Code:
+(id)createObject
{
     return [[[SomeObject alloc]init]autorelease];
}
 
There are no constructors or destructors in Objective-C. You need to be more clear what you're asking about. What is the problem you're trying to solve?

Usually global variables that are alloced in a +initialize method aren't released. The class object itself is never dealloced.
 
There are no constructors or destructors in Objective-C. You need to be more clear what you're asking about. What is the problem you're trying to solve?

Usually global variables that are alloced in a +initialize method aren't released. The class object itself is never dealloced.

So if I alloc an object in an +initialize method I can just not worry about releasing it? and NSObject provides no way of doing so?
 
the init message is not a static method but an instance method.
So dealloc is called on the object as an instance method once its reference count goes to zero.
 
In what cases would you use initialize and not a custom init? and why do you assume that there should be a static destructor?

(To my understanding) +(void) initialize is not just a static version of a custom init method. It doesn't return anything, so you couldn't do something like SomeClass* obj = [SomeClass initialize]. It's purpose is to initialize "class variables". I was trying to make a certain class self-managing, so it had a static array of all the objects of it currently created; so this array would need to be initialized in a static constructor type method like +(void)initialize. (Languages like D and C# use the term static constructor for this type of behavior, I'm just using it to indicate the same type of behavior in objective c).
 
there are these functions
Code:
/* Creating, copying, and freeing instances */

+ new;
+ free;
- free;
+ alloc;
- copy;
+ allocFromZone:(void *)zone;
- copyFromZone:(void *)zone;
- (void *)zone;

that +free object may be what you are looking for, but I haven't played around with it though.
Does anyone else know what it does and how its different to -free ?


EDIT: I just tried these and the free isn't public in the NSObject protocol or interface.
 
So if I alloc an object in an +initialize method I can just not worry about releasing it? and NSObject provides no way of doing so?

If your goal is to create an object in +initialize it will be a global variable. The usual pattern is to make these variables file scope static variables. These are called Class variables in C++ but Objective-C doesn't have that concept. In general, when I do this the pattern is to never release these variables.

However, if you really want to release these variables you can easily do so by writing another class method. Call it whatever you like e.g., +(void)cleanup. You would then call it at an appropriate time.

If your purpose is to cleanup when the app quits then don't bother with that. If you have some other purpose then tell us what it is.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.