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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:02:33+00:00 2026-06-11T20:02:33+00:00

I am using some static instance of a GameScene subclass of CCScene. Calling from

  • 0

I am using some static instance of a GameScene subclass of CCScene. Calling from GameScene

[[CCDirector sharedDirector] replaceScene:[MainMenuScene scene]]; 

doesn’t trigger the dealloc method of the GameScene.

The method is called once I load again the scene (and a new GameScene is created):

+(id) sceneWithId:(int)sceneId
{
    CCScene* scene = [CCScene node];
    GameScene* gameScene = [[self alloc] initWithId:sceneId];
    [scene addChild:gameScene z:0 tag:GameSceneLayerTagGame];
    return scene;
}

-(id) initWithId:(int)sceneId
{
    CCLOG(@"scene With id");
    if ((self = [super init]))
    {
        instanceOfGameScene = self;
        //ONLY NOW the previous object becomes unreferenced and the memory management system is allowed to deallocate it

I wanted to understand if there is a way of forcing the dealloc method to be called every time I replace a (static) scene and not only when the memory gets “freed”.

Or, if I should instead, write some cleanups methods that stops ongoing processes that I don’t want to effect the MainMenuScene (e.g. I have put a stop background music method call in the dealloc method of GameScene but as also the background music is in a static class -and is not added to GameScene as child- then it keeps playing once back in the MainMenuScene).

My quick fix proposal hence is to do something like this:

    [self stopAllStuffInOtherStaticClassesThatAreRelatedOnlyToGameScene];
    [[CCDirector sharedDirector] replaceScene:[MainMenuScene scene]];

Is this a good approach?

EDIT: when is sensible to add this code in the dealloc method?

  [self removeAllChildrenWithCleanup:TRUE];
  • 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-11T20:02:35+00:00Added an answer on June 11, 2026 at 8:02 pm

    I smell bad practice. Never, ever keep a static instance of a node around. Especially not outside the scene hierarchy. It just breaks cocos2d’s way of handling memory management.

    If you need to preserve state, save this state to a separate class but by all means let the scene go when you change scenes. Then restore the state when the scene inits again.

    You can not force the dealloc method. Wanting to do so is a sign of code smell. You can however override the -(void) cleanup method to run de-initialization code before dealloc and after the node has been removed as child.

    EDIT: when is sensible to add this code in the dealloc method?

    [self removeAllChildrenWithCleanup:TRUE];

    Never. Ever.

    Again, if you find you need to do this, there’s a terrible bug somewhere. Cocos2D will have removed the child nodes by this time. In fact, it does so during the cleanup method, one way to cause cocos2d not to remove a node’s children is when you override cleanup and not call [super cleanup].

    Or perhaps when you keep it as a static instance, so it’ll never actually call the cleanup method. And even worse would then continue to run scheduled updates and actions.

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

Sidebar

Related Questions

I'm using HAML to generate some static html pages for a site, and I
So I'm using the ProgressBar JQuery plugin ( http://t.wits.sg/misc/jQueryProgressBar/demo.php ) to create some static
I'm using carbide c++. What to put in .mmp file to link some static
I have some code which I'm currently using to change the static-IP of a
I am using a static UITableview, set up in a storyboard. For some cells
I have refactored some UIView sub-classes into a static library. However, when using Interface
I'm looking for some pros and cons for using extension methods over static utility
Using some of the methods, I am able to check the orientations to which
I am using some easing functions in C# which give Ease In and Ease
I have been using some Javascript to create a text field once an certain

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.