0

I have this setup:

A layer has a sprite as a child. The sprite has this code in its init method:

id fadeOut = [CCFadeOut actionWithDuration:1.0f];
id death = [CCCallFunc actionWithTarget:self selector:@selector(die)];
self.deathAction = [CCSequence actions:fadeOut, death, nil];
[self runAction:deathAction_];

The death action calls the sprite's 'die' method in which its status is changed to 'dead'.

On its update method the layer checks all children and removes the ones wich are 'dead' with:

[self removeChild:child cleanup:YES];

The problem is that the child sprite still has retaincount of 2 after this line. As I understand it is kept by CCCallFunc. If I omit this deathAction and instead remove the sprites that have zero opacity (when they are faded), the code is working and the dealloc method gets called.

How should I remove the sprite by using an action properly?

bolshas
  • 101
  • 3
  • 15

1 Answers1

1

You don't show all of your code. But it seems you are keeping a reference to the action and your likely forgetting to release that.

retainCount is notoriously unreliable : link so please don't use it and certainly don't count on it to be accurate.

Community
  • 1
  • 1
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • You were right about the reference. I amended the code so that the removal is handled internally by the sprite. I call the death method through the action and in it I call `[self removeFromParentAndCleanup:YES];` The problem was that before this call I hade to call `[deathAction_ release];`. Why? I have the release call in the dealloc method of the sprite? What if I would like to sometimes remove the sprite somehow otherwise than through the action? – bolshas Sep 11 '11 at 20:28
  • If you are releasing an object without knowing exactly which retain you are balancing, you have only hidden a symptom, not fixed the problem. – bbum Sep 12 '11 at 06:22