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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:44:00+00:00 2026-05-25T14:44:00+00:00

I’m trying to write my little app and experiencing some memory management problems. At

  • 0

I’m trying to write my little app and experiencing some memory management problems.
At first, I have Game singleton object with property:

//Game.h
@interface Game : NSObject

@property (nonatomic, retain) MapBuildingsLayer *mapBuildingsLayer;

+(Game *) game;
-(BOOL) addObject:(NSString *) objName At:(CGPoint) pt;

@end

where MapBuildingsLayer is just cocos2d CCLayer instance

//Game.m
@implementation Game
@synthesize mapBuildingsLayer = _mapBuildingsLayer;

static Game *instance = nil;

+ (Game *)game {
    @synchronized(self) {
        if (instance == nil) {
            instance = [[Game alloc] init];
        }
    }
    return instance;
}

-(BOOL) addObject:(NSString *)objName At:(CGPoint)pt
{
    if([objName isEqualToString:OBJ_TYPE_PIT])
    {
        if([[Game game].mapBuildingsLayer addPitAt:pt]) //app crashes here
        {
            [self toggleConstructionMode];
            return YES;
        }
    }
    return NO;
}

@end

In MapBuildingsLayer.m‘s init method I use Game‘s mapBuildingsLayer property to store a reference to this CCLayer instance in my singleton (for future use in other methods):

//MapBuildingsLayer.m
@implementation MapBuildingsLayer

-(id) init
{
    if( (self=[super init])) {
        [Game game].mapBuildingsLayer = self;
    }
    return self;
}

When I call Game‘s addObject:objName At: method, my app crashes with EXC_BAD_ACCESS.
How I must declare property in Game singleton to use it from other places in my app’s lifetime?

  • 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-25T14:44:00+00:00Added an answer on May 25, 2026 at 2:44 pm

    You typically don’t use the singleton in the class itself, try changing

            if([[Game game].mapBuildingsLayer addPitAt:pt]) //app crashes here
    

    to

            if([self.mapBuildingsLayer addPitAt:pt]) //app crashes here
    

    You should be using [Game game] external to the class to get into the singleton instance of your class and call its methods, but internal to the class you would just refer to it as self like normal.

    Typically if you’re going to use a game singleton this isn’t how you’d use it. Try thinking of it like this with a giant state machine, Create a CCScene subclass which will initialize all of your respective CCLayer subclasses and control them. Then from the statemachine you can load the appropriate initializing its scene and that will create everything under it.

    In your applicationDidFinishLaunching method you simply have your singleton object load your first scene. I’d really recommend checking out the Learning Cocos2d Book as it covers this singleton state engine very well and I think would clear up all your questions.

    Bottom line is have the state engine load the scene which loads the layers.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.