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

  • Home
  • SEARCH
  • 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 7873485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:31:40+00:00 2026-06-03T02:31:40+00:00

Short Version: I define a property with (nonatomic, retain) and assumed that the property

  • 0

Short Version:

I define a property with (nonatomic, retain) and assumed that the property would be retained. But unless I call retain when assigning a dictionary to the property, The app crashes with an EXEC BAD ACCESS error.

Long Version:

I have a singleton which has a dictionary. The header is defined like this

@interface BRManager : NSObject {
}

@property (nonatomic, retain) NSMutableDictionary *gameState;

+ (id)sharedManager;
- (void) saveGameState;

@end

In the implementation file, I have a method that’s called in the init. This method loads a plist form the bundle and makes a copy of it in the users documents folder on the device.

- (void) loadGameState
{
 
    NSFileManager *fileManger=[NSFileManager defaultManager];
    NSError *error;
    NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
    NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"gameState.plist"];
    NSLog(@"plist path %@",destinationPath);    
    
    if (![fileManger fileExistsAtPath:destinationPath]){
        
        NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"gameStateTemplate.plist"];
        [fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
        
        gameState = [NSMutableDictionary dictionaryWithContentsOfFile:sourcePath];
                
    }else{
        gameState = [NSMutableDictionary dictionaryWithContentsOfFile:destinationPath];
    }
    
}

Now here’s how I thought this should work. In the header I define the gameState property with (nonatomic, retain). I assumed (probably incorrectly) that ‘retain’ meant that the gameState dictionary would be retained. However, I have another method in my singleton (saveGameState) that get’s called when the AppDelegate -> ‘applicationWillResignActive‘.

- (void) saveGameState
{
    NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
    NSString *plistPath = [doumentDirectoryPath stringByAppendingPathComponent:@"gameState.plist"];
    [gameState writeToFile:plistPath atomically:YES];
}

This throws an EXEC BAD ACCESS error on gameState. If I modify loadGameState to retain the gameState dictionary, everything works as it should. eg:

gameState = [[NSMutableDictionary dictionaryWithContentsOfFile:sourcePath] retain];

I’m guessing this is the correct behaviour, but why? Does (nonatomic, retain) not mean what I think it means, or is something else at play here?

I’ve not really grok’d memory management yet, so I stumble on this stuff all the time.

  • 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-06-03T02:31:42+00:00Added an answer on June 3, 2026 at 2:31 am

    You must use the accessor:

    self.gameState = [NSMutableDictionary dictionaryWithContentsOfFile:sourcePath];
    

    or (is equivalent to):

    [self setGameState:[NSMutableDictionary dictionaryWithContentsOfFile:sourcePath]];
    

    instead of

    gameState = [NSMutableDictionary dictionaryWithContentsOfFile:sourcePath];
    

    which only sets the ivar without any notion of property.

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

Sidebar

Related Questions

Short version: How would i go about creating an object pool that can store
Short Version: What are the chances that a site's various sub-pages' page-ranks will suffer
Short version question : Is there navigator.mozIsLocallyAvailable equivalent function that works on all browsers,
The short version is that I have a protocol which has an optional parameter.
Short version Need to distribute nightly builds to 70+ people each morning, would like
Short Version The MSDN documentation for Type.GetProperties states that the collection it returns is
Short version : echo testing | vim - | grep good This doesn't work
Short version: I have a similar setup to StackOverflow. Users get Achievements. I have
Short version: Is it easy/feasible/possible to program modal window in Flash (AS3)? Is there
Short version: I'm trying to determine the best way to track what the user

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.