static destructors in objective C

Discussion in 'iOS Programming' started by alecbenzer, May 2, 2010.

  1. alecbenzer macrumors newbie

    Joined:
    May 2, 2010
    #1
    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.
     
  2. Avizzv92 macrumors regular

    Joined:
    Mar 23, 2008
    #2
    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];
    }
    
     
  3. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. alecbenzer thread starter macrumors newbie

    Joined:
    May 2, 2010
    #5
    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?
     
  5. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #6
    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.
     
  6. alecbenzer thread starter macrumors newbie

    Joined:
    May 2, 2010
    #7
  7. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #8
  8. alecbenzer thread starter macrumors newbie

    Joined:
    May 2, 2010
    #9
    (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).
     
  9. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #10
    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.
     
  10. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    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.
     

Share This Page