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 6346023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:56:20+00:00 2026-05-24T20:56:20+00:00

I get an error when running my code. The culprit is me accessing a

  • 0

I get an error when running my code. The culprit is me accessing a string from a plist below:

    NSString *sImageFile = [dictionary objectForKey:@"answerCorrect"];
    NSLog(@"%@",sImageFile);

I have this in my cocos2d Init shown here:

-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {

        NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
        NSString *ctlLang = [sud valueForKey:@"ctlLang"];
        NSNumber *questionState = [sud valueForKey:@"questionState"];
        NSNumber *scoreState = [sud valueForKey:@"scoreState"];
        gameArray = (NSMutableArray *)[sud valueForKey:@"gameArray"];
        for (NSString *element in gameArray) {
            NSLog(@"\nQL gameArray value=%d\n", [element intValue]);
        }
        NSString *path = [[NSBundle mainBundle] bundlePath];
        NSString *finalPath = [path stringByAppendingPathComponent:ctlLang];
        dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];

        NSString *sImageFile = [dictionary objectForKey:@"answerCorrect"];
        NSLog(@"%@",sImageFile);
    }
}

The printing of the string works fine in the init section of the scene. The problem occurs in a method I define later. For some reason it is not returning the string in the method shown here:

-(void) checkAnswer: (id) sender {

    CGSize size = [[CCDirector sharedDirector] winSize];
    CCMenuItemSprite *sAnswer = (CCMenuItemSprite *)sender;
    NSLog(@"Checking Answer Tag is ---> %d",sAnswer.tag);
    NSString *sImageFile = [dictionary objectForKey:@"answerCorrect"];
    NSLog(@"%@",sImageFile);
    if ([question.answer integerValue] == sAnswer.tag) {
        //...
    }
}

What am I missing here? the program bombs at the NSLog statement.

  • 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-24T20:56:21+00:00Added an answer on May 24, 2026 at 8:56 pm

    You assign the object returned by dictionaryWithContentsOfFile: to the dictionary ivar but you do not claim ownership of it by sending it a retain message to it:

    dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];
    

    The method dictionaryWithContentsOfFile: returns an object you do not own. Probably, by the time checkAnswer: is executed the object has already been deallocated. You need to retain it:

    dictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
    

    Or use alloc-initWithContentsOfFile: instead, which returns an object you own:

    dictionary = [[NSDictionary alloc] initWithContentsOfFile:finalPath];
    

    And the same goes for the gameplay ivar. You do not own the object returned by valueForKey: and you need to retain it. So this line:

    gameArray = (NSMutableArray *)[sud valueForKey:@"gameArray"];
    

    should be:

    gameArray = [[sud valueForKey:@"gameArray"] retain];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will get the error below randomly when I'm running an asp.net application I
I'm running the PHP code below from command line. The issue is, its memory
Hi I get an error running this code in mono tools? Unsure how to
I get this error when running my code, and I don't receive anything back
While running my code I get the error TypeError: Error #1009: Cannot access a
I get this 'error' when running PEVerify on a custom generated assembly. [MD](0x8013124C): Error:
I get the following error when running my Visual Studio 2008 ASP.NET project (start
We get the following error when running at test: ContextSwitchDeadlock was detected Message: The
I get the following error when running FXCop: CA1800 : Microsoft.Performance : 'obj', a
When running my application I sometimes get an error about too many files open

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.