1

I have a -dealloc() method which I am assuming I can use to dealloc instance variables, I have another variable that is not in the instance, rather a class level variable, wondering when & how I dealloc this? I can't do it in the instance method dealloc() right? Code below for reference (on varaible: levelHash):

@interface Level : CCNode 
    { 
        //Instance variables
        PlayBackgroundLayer* playBGLayer;
        PlayTilemapLayer* playTilemapLayer;
        PlayUILayer* playUILayer;
        PlayElementLayer* playElementLayer;
    }
    //Property declarations for instance variables
    @property (nonatomic, retain) PlayBackgroundLayer* playBGLayer;
    @property (nonatomic, retain) PlayTilemapLayer* playTilemapLayer;
    @property (nonatomic, retain) PlayUILayer* playUILayer;
    @property (nonatomic, retain) PlayElementLayer* playElementLayer;

    //Static methods
    +(void) Initialize: (NSString*) levelReference;
    +(void) InitLevel: (NSString*) levelReference;
    +(Level*) GetCurrentLevel;
@end

//static variables
NSMutableDictionary *levelHash;

and my implementation:

+(void) Initialize: (NSString*) levelReference 
{
    levelHash = [[NSMutableDictionary alloc] init];
    [levelHash setObject:NSStringFromClass([LevelOne class]) forKey:@"1"];
    //EG CALL IT [levelHash objectForKey:@"foo"];
    //WHEN DO I CALL THIS??? [levelHash release];
}
williamsandonz
  • 15,864
  • 23
  • 100
  • 186

1 Answers1

2

Classes aren't deallocated for the life of your program, so there doesn't seem to be much point in releasing that dictionary. All the memory your app uses is reclaimed when it terminates. You can create a "tear down" method for the class where you release the dictionary if you want to, just as you've created a custom initialization method.

(By the way, it is neither a class nor a static variable; ObjC doesn't have class variables and, lacking the static keyword, it's actually global. This is why there's no need to worry about a leak -- global variables also exist for the entire lifetime of your program.

Also, you shouldn't be putting it in your header file, as I mentioned earlier. Every file that imports this header is going to be re-defining it, which will cause a linker error -- you can only define things once.)

Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195
  • Technically it is outside the interface so it drops to a C variable which would mean there would only be one and it would act like a static variable wouldn't it? – Ben Trengrove Jun 13 '12 at 05:45
  • It acts like a C variable, yes, but not like a static C variable. Every file that imports this header is going to re-declare storage for that variable, which is an error. – jscs Jun 13 '12 at 05:46
  • Isn't that dealloc() called when every instance is destroyed? Therefore if one instance was destroyed would it not dealloc the global var? – williamsandonz Jun 13 '12 at 05:51
  • do I have +dealloc() or -dealloc()? – williamsandonz Jun 13 '12 at 05:52
  • @Bacon: I've edited my answer a few times as I looked more carefully at your question. There's no `+dealloc`; you're right that it's only for instances. You may be responding to an older version of my answer. I don't really understand your design. – jscs Jun 13 '12 at 05:52
  • Hey Josh, thanks for the update. Ok I think I've now grasped it, when you say "doesn't seem to be much point..", that makes sense to me, I guess when the App closes, those variables are released from memory anyway. Thankyou!. Will move it out of the header file also. First time writing a cocos2d/Objective-c game so yes maybe my design doesn't make sense! haha :) – williamsandonz Jun 13 '12 at 05:59
  • Right, all the memory your app uses is reclaimed when it terminates. There's no worry about a leak here, since you always have that pointer. – jscs Jun 13 '12 at 06:01
  • @josh tell me one thing, If we declare a variable in .m file then it is directly usable by the static method. and static method can deal only with the static variable. why it happen . – gauravds Jun 13 '12 at 06:29
  • @Gaurav: It's directly usable by any method or function in that file, and in any method or function in any other file that can see that file. This is something that ObjC gets from C. – jscs Jun 13 '12 at 06:30
  • and why that variable init once? – gauravds Jun 13 '12 at 06:32
  • @Gaurav_D_Sharma: I don't understand your question. – jscs Jun 13 '12 at 06:33
  • variable which declared in .m file, without declare static keyword, initialize once and this is the behavior of static variables, isn't it. – gauravds Jun 13 '12 at 06:35
  • @Gaurav_D_Sharma: It's the behavior of global variables. – jscs Jun 13 '12 at 06:40
  • where u read that global variable initialize once. global variable initialize many times, and accessible from outside. that's why they called it global. Static variable initialize once and used inside the static methods or instance methods both. static methods can excess only static methods. Please correct my logic : .m declared variable is not static by the above conclusion. and If u say that .m declared variable are global then why it is not direct accessible by some other class – gauravds Jun 13 '12 at 07:01
  • @Gaurav: http://stackoverflow.com/questions/959889/ A global variable is accessible from any file that can see it. There's [no such thing as static methods in ObjC](http://stackoverflow.com/a/8089623/). Please stop asking me the same thing over and over. Do some research of your own. – jscs Jun 13 '12 at 07:06