2

What am I doing wrong? Am I encoding/decoding the enum type properly?

GameSettings interface:

typedef enum {
    CatWhite = 0,
    CatBlack = 1,
    CatOrange = 2
} CatColor;

...
CatColor catColor;
...
@property CatColor catColor;

GameSettings implementation:

@synthesize catColor;

...

+ (GameSettings*)GetInstance
{
    if (sharedSingleton == nil) {
        sharedSingleton = [[super allocWithZone:NULL] init];
        sharedSingleton.catColor = CatWhite;
    }
    return sharedSingleton;
}
    -(void)encodeWithCoder:(NSCoder *)coder {
        [coder encodeInt:self.catColor forKey:@"CatColor"];
    }

    -(id)initWithCoder:(NSCoder *)coder {
        if((self = [super init])) {
            self.catColor = [coder decodeIntForKey:@"CatColor"];
        }
NSLog(@"initWithCoder: %d", self.catColor); //this logs the correct int
        return self;
    }

AppDidFinishLaunching:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *data = [defaults objectForKey:@"GameSettings"];
        GameSettings *gGameData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        if(gGameData != NULL) {
            GameSettings *settings = [GameSettings GetInstance];
            settings.catColor = gGameData.catColor;

        }else {
            GameSettings *settings = [GameSettings GetInstance];
            settings.catColor = CatWhite;
        }
        ...
    }

Save settings:

GameSettings* settings = [GameSettings GetInstance];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:settings];
[defaults setObject:data forKey:@"GameSettings"];
[defaults synchronize];

The crash (Program received signal: “EXC_BAD_ACCESS”) comes when I re-launch the app and try to access the game settings:

GameSettings* settings = [GameSettings GetInstance];
NSLog(@"settings: %d", settings); //EXC_BAD_ACCESS
NSLog(@"catColor: %d", settings.catColor); //EXC_BAD_ACCESS

Why can't I access the GameSettings singleton after a re-launch?

sol
  • 6,402
  • 13
  • 47
  • 57
  • I'm assuming that GameSettings inherits from NSObject, but is that the case? Figure I ought to ask. Doesn't really help that your trimming out bits of the interface makes most of it out of context, either. Also, what does `GetInstance` look like? –  Feb 24 '11 at 02:19
  • Can you post the stack trace from the debugger console? – ughoavgfhw Feb 24 '11 at 02:25
  • @nil, yes, it inherits from NSObject. I have added the Get Instance method above. – sol Feb 24 '11 at 18:10

2 Answers2

0

Are you sure you want to use applicationDidFinishLaunching there? When you resurrect the program, it may be invoking awakeFromNib instead of applicationDidFinishLaunching (source: http://www.cimgf.com/2008/03/26/cocoa-tutorial-awakefromnib-vs-applicationdidfinishlaunching/ ).

gpcz
  • 806
  • 6
  • 8
0

Finally figured this out. GameSettings was getting deallocated because NSKeyedUnarchiver was autoreleasing it:

GameSettings *gGameData = [NSKeyedUnarchiver unarchiveObjectWithData:data];

Fix:

GameSettings *gGameData = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain];
sol
  • 6,402
  • 13
  • 47
  • 57