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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:25:38+00:00 2026-05-19T22:25:38+00:00

I’m trying to get Cocos2D on the iPhone to clean itself up before I

  • 0

I’m trying to get Cocos2D on the iPhone to “clean itself up” before I switch back to a UIView-based view in my iPhone app, but it’s overreleasing (or, I am overreleasing) something and crashing, and I can’t make heads or tails of it.

This is somewhat long, but I’ve tried to take care to organize it.

My node hierarchy looks like this, and the parenthesis indicate “what’s in” each node:

  1. CCScene (Menu)
  2. CCLayer (Character)
  3. CCLayer (Whole Animation)
  4. CCSprite, CCSpriteBatchNode (Parts of Animation, there can be many of each type)

So, as the “Character” runs different animations, I remove the “animation” CCLayer from the “character” CCLayer, create a new “animation” CCLayer, and add it as a child. So far, that’s caused no problems.

Finally, there’s a button on CCScene that “ends” the Cocos part of the app. I want to return to UIKit-land when I tap the “End” button.

However, before I return back to UIView-land, I want to run one final animation on the Character, and when THAT is finished, terminate. To do that, I “register” a handler on the character CCLayer like this. I call the “final” animation, and then callback to the CCScene when the final animation is done (using KVO):

- (void) doEndWithHandler:(id<LWECharacterDelegate>)handler
{
  // Handler is the CCScene, "self.parent" could work but I want it loosely coupled
  self.endOfSessionHandler = handler;

  // Tell character to start final animation -- this creates a new CCLayer,
  // starts the animation, and assigns that CCLayer into self.animatedSequence
  [self changeCharacterActionTo:END_ANIMATION key:nil]];

  // The "animation" CCLayer has a property called "moving" -- observe it
  [self.animatedSequence addObserver:self forKeyPath:@"moving" options:NSKeyValueObservingOptionNew context:NULL];
}

And then my observing code, which is called back to when “moving” becomes NO (=animation finishes):

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
  if ([keyPath isEqualToString:@"moving"])
  {
    BOOL movingStatus = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
    if (movingStatus == NO)
    {
      // Stop observing
      [object removeObserver:self forKeyPath:@"moving"];

      // Stop all animations on this level
      [self.animatedSequence stopAnimation];
      [self removeChild:self.animatedSequence cleanup:YES];
      [self removeFromParentAndCleanup:YES];

      // Handler callback
      if (self.endOfSessionHandler && [self.endOfSessionHandler respondsToSelector:@selector(characterDidFinishSession)])
      {
        [self.endOfSessionHandler characterDidFinishSession];
      }
    }
  } // if key = moving
}

Now, you may say,

Didn’t know you that you can just add a callback using CCCallFunc as an action on your animation at the end, so you know when you’re done moving?

Yes, I do know that – but the point is that the CCScene “knows” when the end button is pressed — not any one specific animation. The CCAction is already in motion when the end button is pressed, so I want to tell all sprites to STOP animating and destroy.

My animation CCLayer has some special code to tell me when the sprite(s) has stopped moving. That code is working well– I use a CCCallFunc callback on the end of every animation to tell my “animation” CCLayer class that it’s done.

Why it seems to be a problem, though, is that I get the KVO notification that “moving” has changed BEFORE the Cocos2D action stack trace has unwound. I’m pretty sure my problem is somewhere in there, because as soon as the KVO notification comes through, I try to stop everything (see the code above). Yet, not everything stops, because the Cocos2D framework crashes (overrelease) as it tries to “wrap up” the stack trace.

Is it not possible to stop an animation from within a CCCallFunc callback that is an action animating the same sprite?

For you true Cocos-heads out there, the exact line that is crashing is:

if( currentTarget->currentActionSalvaged ) {
  // The currentAction told the node to remove it. To prevent the action from   
  // accidentally deallocating itself before finishing its step, we retained
  // it. Now that step is done, it's safe to release it.    
    [currentTarget->currentAction release];

.. which is on line 327 of CCActionManager.m.

  • 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-19T22:25:39+00:00Added an answer on May 19, 2026 at 10:25 pm

    All right, I solved this one on my own. The key lesson here was this:

    If you are “cleaning up” a Cocos session, you should be fine to do it FROM a Cocos callback (CCCallFunc), but do NOT call [[CCDirector sharedDirector] end] until after your Cocos callback stack trace has unwound.

    That is all.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.