Question

I've got a weird issue here. I'm creating a game in Sprite Kit and when I first initialize all the variables it seems to work fine. I have a singleton class that holds all the data that I will use in the game. This singleton holds one object (the World) and the World object holds arrays of objects that I use throughout the game.

After initialization, I try to print out what the World object contains in a certain array. Below is an example:

NSLog(@"Game manager ports: %@", [IPGameManager sharedGameData].world.ports);

However, when this is run, the simulator hangs. I haven't tried it on a real device yet (don't have the developer account yet). However, when I print the same thing right after the initialization of the World 'ports', it prints fine. Here's the header file of the World object:

@interface IPWorld : NSObject <NSCoding>

@property (nonatomic, retain) IPPlayer* player;
@property (nonatomic, retain) NSMutableArray* ports;
@property (nonatomic, retain) NSMutableArray* crewMembers;
@property (nonatomic, retain) NSMutableArray* ships;
@property (nonatomic, retain) NSMutableArray* quests;

@end

I wouldn't think the 'retain' or anything would cause this but I am not sure what the issue is. Any help is greatly appreciated. Thanks!

EDIT:: Below is the code of my singleton:

+(instancetype)sharedGameData {
    NSLog(@"Loading sharedGameData");
    static id sharedInstance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"should only see this once");
        sharedInstance = [self loadInstance];
    });

    return sharedInstance;
 }

+(instancetype)loadInstance {
    NSData* decodeData = [NSData dataWithContentsOfFile:[IPGameManager filePath]];
    if (decodeData) {
        IPGameManager* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodeData];
        return gameData;
    }
    return [[IPGameManager alloc] init];
}

+(NSString*)filePath {
    static NSString* filePath = nil;
    if (!filePath) {
        filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"gamedata"];
    }
    return filePath;
}
Was it helpful?

Solution

To debug any hang, the first thing you want are the stack traces of all threads. When this happens in iOS simulator, press the "Pause" button in Xcode and the view the stack traces. You can also, in the debugger window, type t a a bt (which means thread apply all backtrace) to dump the stack traces of all threads.

Once you have that, see what your main thread is blocked on (e.g., deadlock, blocking i/o, infinite recursion).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top