Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3990936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:33:16+00:00 2026-05-20T06:33:16+00:00

I have a problem. There is class to store game progress: struct GameData {

  • 0

I have a problem. There is class to store game progress:

struct GameData {
    PackData & packDataById(LEVEL_PACK packId);
    int gameVersion;
    AudioData audio;
    PackData sunrise;
    PackData monochrome;
    PackData nature;
};

//singleton
@interface GameDataObject : NSObject <NSCoding>
{
    GameData data_;
}
+(GameDataObject*) sharedObject;
-(id) initForFirstLaunch;
-(GameData*) data;
-(void) save;
@end

and implementation:

@implementation GameDataObject

static GameDataObject *_sharedDataObject = nil;

+ (GameDataObject*) sharedObject
{
    if (!_sharedDataObject) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *encodedObject = [defaults objectForKey:Key];
        if (!encodedObject)  {
            _sharedDataObject = [[GameDataObject alloc] initForFirstLaunch];
        }
        else {
            _sharedDataObject = (GameDataObject*)[NSKeyedUnarchiver unarchiveObjectWithData: encodedObject];
        }
    }
    return _sharedDataObject;
}

-(GameData*) data
{
    return &data_;
}

-(id) initForFirstLaunch
{
    self = [super init];
    if (self) {
        data_.audio.reset();
        data_.sunrise.reset();
        data_.monochrome.reset();
        data_.nature.reset();
        data_.gameVersion = 1;
        data_.sunrise.levelData[0].state = LEVEL_OPENED;
    }
    return self;
}

-(void) save
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:self] forKey:Key];
    [defaults synchronize];
}

-(void) encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInt:data_.gameVersion forKey:@"game-version"];
    [encoder encodeBytes:(uint8_t*)&data_.audio length:sizeof(AudioData) forKey:@"audio-data"];
    [encoder encodeBytes:(uint8_t*)&data_.sunrise length:sizeof(PackData) forKey:@"sunrise-pack"];
    [encoder encodeBytes:(uint8_t*)&data_.monochrome length:sizeof(PackData) forKey:@"monochrome-pack"];
    [encoder encodeBytes:(uint8_t*)&data_.nature length:sizeof(PackData) forKey:@"nature-pack"];
}

-(id) initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        data_.gameVersion = [decoder decodeIntForKey:@"game-version"];
        NSUInteger length = 0;
        {
            const uint8_t *buffer = [decoder decodeBytesForKey:@"audio-data" returnedLength:&length];
            assert(length);
            memcpy(&data_.audio, buffer, length);
        }

        {
            const uint8_t *buffer = [decoder decodeBytesForKey:@"sunrise-pack" returnedLength:&length];
            assert(length);
            memcpy(&data_.sunrise, buffer, length);
        }

        {
            const uint8_t *buffer = [decoder decodeBytesForKey:@"monochrome-pack" returnedLength:&length];
            assert(length);
            memcpy(&data_.monochrome, buffer, length);
        }

        {
            const uint8_t *buffer = [decoder decodeBytesForKey:@"nature-pack" returnedLength:&length];
            assert(length);
            memcpy(&data_.nature, buffer, length);      
        }
    }
    return self;
}

@end

It loads and saves itself correctly when save is called directly after initialization and nothing more is done.

But when I try a simple thing. I write in appDidFinishLaunching

GameDataObject *obj = [GameDataObject sharedObject]; 

Then everything is done – just one simple menu is loaded, and I minimize the application so

-(void) applicationDidEnterBackground:(UIApplication*)application
{
    [[CCDirector sharedDirector] stopAnimation];
    [[GameDataObject sharedObject] save];
}

is executed. And in this method obj is totally corrupted (before saving), sometimes it’s even seen with debugger as another class object.

What am I doing wrong?

EDIT

Just launching the app and minimizing it causes the same problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:33:17+00:00Added an answer on May 20, 2026 at 6:33 am

    As I already mentioned in the comment, you have a memory bug when unarchiving the data using NSKeyedUnarchiver.

    The method +[NSKeyedUnarchiver unarchiveObjectWithData:] returns an autoreleased object (you can tell from the naming convention: it doesn’t contain either new, alloc or copy) so you’d have to take ownership of the object by sending it a retain message. Now the object won’t be released by the autorelease pool at the end of the runloop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem. There are two classes: struct Base { Base* retain() {
i have problem to store image into sd car, there are not display file
i have problem with libhid . i found that there 2 way 4 accessing
I have problem with my application. I have table report, there are 2 column
I'm constructing a form. No problem there. Aside of a submit button, I have
I have some problem with Android AsyncTask. There is an Activity which contains some
I have a problem with an MS Chart chart type column. If there are
I have a problem with sorting NSTableColumn contents. In my NSTableView there are three
I have a problem downloading particular file types by WebClient. So there are no
I have one problem with android calendar programming, so If there are 2 months

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.