I'm trying to write my little app and experiencing some memory management problems.
At first, I have Game
singleton object with property:
//Game.h
@interface Game : NSObject
@property (nonatomic, retain) MapBuildingsLayer *mapBuildingsLayer;
+(Game *) game;
-(BOOL) addObject:(NSString *) objName At:(CGPoint) pt;
@end
where MapBuildingsLayer
is just cocos2d CCLayer
instance
//Game.m
@implementation Game
@synthesize mapBuildingsLayer = _mapBuildingsLayer;
static Game *instance = nil;
+ (Game *)game {
@synchronized(self) {
if (instance == nil) {
instance = [[Game alloc] init];
}
}
return instance;
}
-(BOOL) addObject:(NSString *)objName At:(CGPoint)pt
{
if([objName isEqualToString:OBJ_TYPE_PIT])
{
if([[Game game].mapBuildingsLayer addPitAt:pt]) //app crashes here
{
[self toggleConstructionMode];
return YES;
}
}
return NO;
}
@end
In MapBuildingsLayer.m
's init
method I use Game
's mapBuildingsLayer
property to store a reference to this CCLayer instance in my singleton (for future use in other methods):
//MapBuildingsLayer.m
@implementation MapBuildingsLayer
-(id) init
{
if( (self=[super init])) {
[Game game].mapBuildingsLayer = self;
}
return self;
}
When I call Game
's addObject:objName At:
method, my app crashes with EXC_BAD_ACCESS
.
How I must declare property in Game
singleton to use it from other places in my app's lifetime?