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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:43:57+00:00 2026-06-12T21:43:57+00:00

Shame on me for this, but yes. I am having problems with this even

  • 0

Shame on me for this, but yes. I am having problems with this even if I am using ARC.

My main question is: am I having a circular reference in my code?

My code description:

I have a game scene with a series of child nodes (background, playerEntity, inputlayer). The issue is that when I trigger game over and replace the scene the scene would not deallocate (I added a dealloc method in the game scene even if not needed as I am using ARC).

I believe that this is due to the fact that in the child nodes I refer to the game scene and in the game scene I refer to them, creating a circular reference.

E.g. In the game scene update method I call the following:

-(void) update:(ccTime)delta
{
bool isGamePaused = [GameController sharedGameController].isGamePaused;

if(isGameOverInProgress==true){
    //Do whatever game over animation u like
}
else if(isGamePaused==false)
{
    elapsedTime+=delta;

    //Update input layer
    InputLayerButtons *inputLayer = (InputLayerButtons *) [self getChildByTag:InputLayerTag];
    [inputLayer update:delta];

    //Move background
    ParallaxMultipleBackgroundOneBatchNode* background = (ParallaxMultipleBackgroundOneBatchNode*) [self getChildByTag:BackgroundNodeTag];
    [background update:delta];

    //verify enemies bullet collision
    [self verifyPlayerBulletCollision];
    [levelSprites update:delta];

    //Update bullets position
    [bulletCache updateVisibleBullets:delta];

I know that is a kind of crazy approach but I wanted to avoid scheduling updates in the child nodes in order to have full control in the ShooterScene class of what was happening in the game. I don’t use a semi-singleton patter for the ShooterScene class and instead, I have a semi-singleton GameController class instance that I use to communicate the game state between nodes.

But in some child nodes I had to refer to the parent ShooterScene because for example using the semisingleton GameController class to store the ship position turned out in a too slow response (e.g. reading the position in the update method and updating the ship accordingly proved much slower than updating it directly as I do in here). Hence my approach is not 100% clean as I wanted it to be (I wanted to avoid all references to parent classes to avoid the issue of circular references and also to keep control of all events in the ShooterScene class).

So, as example, here is how I update the ship position from the input layer (namely InputLayerButtons):

-(void) update:(ccTime)delta
{
    // Moving the ship with the thumbstick.
    ShooterScene * shooterScene = (ShooterScene *) [self parent];
    ShipEntity *ship = [shooterScene playerShip];
    delta = delta * 2.0f;

    CGPoint velocity = ccpMult(joystick.velocity, 200);
    if (velocity.x != 0 && velocity.y != 0)
    {
        ship.position = CGPointMake(ship.position.x + velocity.x * delta, ship.position.y + velocity.y * delta);
    }     
}

I thought that it was ok to refer to the parent scene and that this would not cause a circular reference. But it seem to happen, so I am confused on this.

Here is the code of the game over method, the dealloc method of ShooterScene is never called (cleanup instead is called):

   [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:4.0 scene:[MainMenuScene scene] withColor:ccc3(255, 255, 255)]];

Is there something in replaceScene that I don’t understand? I couldn’t find the code for replaceObjectAtIndex so here I post only the Cocos2d 2.0 code for replaceScene

-(void) replaceScene: (CCScene*) scene
{
    NSAssert( scene != nil, @"Argument must be non-nil");

    NSUInteger index = [scenesStack_ count];

    sendCleanupToScene_ = YES;
    [scenesStack_ replaceObjectAtIndex:index-1 withObject:scene];
    nextScene_ = scene; // nextScene_ is a weak ref
}

I tried to see if the dealloc method would be called in other scenes (simpler that ShooterScene and with no potential circular references) and it did worked. So I am pretty confident that it is due to circular references but how can I detect the piece of code that makes this happen?

  • 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-12T21:43:58+00:00Added an answer on June 12, 2026 at 9:43 pm

    ARC does not prevent retain cycles. Using zeroing weak references however greatly decreases the chances of creating a retain cycle.

    As a general rule of thumb regarding cocos2d node hierarchy & retain cycles:

    You do not have a retain cycles if you:

    • keep a strong reference to a child (or grandchild) node

    Yes, that’s the only rule that’s safe in terms of retain cycles in cocos2d! That means that references to nodes which are not children or grandchildren of the node should never be strong (retaining) references.

    You are guaranteed to have a retain cycle if you:

    • keep a strong reference to a parent (or grandparent) node
    • keep a strong reference to a sibling node, and the sibling node keeps a strong reference to the original node either directly or indirectly

    You may have a retain cycle if you:

    • keep a strong reference to a node that’s not a child (or grandchild) node, depending on whether that node holds a strong reference back to the original node or other factors.

    In your case that means by keeping a strong reference to the parent you created a retain cycle. Under ARC every ivar is a strong reference by default. You can do two things to resolve this and any other potential retain cycle:

    • change the property to be weak or the ivar to be __weak (requires iOS 5.0 as minimum deployment target – highly recommended)
    • set the ivar to nil in the node’s -(void) cleanup method

    How to create weak references:

    // weak references to parent nodes are safe:
    @interface MyClass : CCNode
    {
        __weak CCLayer* _gameLayer;
    }
    @property (nonatomic, weak) CCScene* gameScene;
    @end
    

    How to cleanup properly:

    -(void) cleanup
    {
        _strongRefToNonChildNode = nil;
        [super cleanup];
    }
    
    -(void) momCallsShesAlmostHere
    {
        [self cleanupApartment];  // just kidding :)
    }
    

    Cleaning up in dealloc as one might think would be appropriate is too late for resolving retain cycles. Because if you have a retain cycle, you obviously can’t resolve it in dealloc since the retain cycle prevents the objects from deallocating in the first place.

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

Sidebar

Related Questions

I'm using ASP.NET MVC2 and C#, but this question applies to ASP.NET in general.
I'm having the exact same problem as in this question: Gray border when using
I am having the same problem as this post but the answer does not
Note: This is similar to this question but it is not the same. I
Pretty much the same as this question. But I can't seem to get this
I have an ObjectDataSource (but perhaps this question is the same for all kinds
I must be using @Autowired wrong, but I don't get exactly how. This is
I am having problems loading specific ViewControllers on the tabBarController. This is what my
Of the many problems I've been having with my current app, this is one
I bought a new Vista PC recently but was having lots of problems getting

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.