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];
}
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
statickeyword, 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.)