Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Memory leak on CCTouchableSprite #3

Closed
yangboz opened this Issue Dec 26, 2012 · 6 comments

Comments

Projects
None yet
2 participants

yangboz commented Dec 26, 2012

While putting kinds of CCSprite on CCLayer, after replace scene, every CCSprite could be release,but using CCTouchableSprite to replace the CCSprite, the sprite reference still in memory, any idea?

For example,at XCode console,it gives difference result:

// CCTouchableSprite *_bear;
CCSprite *_bear;

// _bear = [CCTouchableSprite spriteWithSpriteFrameName:defaultFrameName];
_bear = [CCSprite spriteWithSpriteFrameName:defaultFrameName];

m1entus added a commit that referenced this issue Dec 26, 2012

Owner

m1entus commented Dec 26, 2012

It was possible that CCTouchSprite delegate wasn't removed. I propably fixed it.

@m1entus m1entus closed this Dec 26, 2012

yangboz commented Dec 27, 2012

Verified, only using CCTouchableSprite declare,no more memory leak!

And another memory leak meets up while using block code as your sample code:

// [_bear setTouchBlock:^(CCTouchableSprite *sprite) {
// [_bear stopAction:_walkAction];
// [[SimpleAudioEngine sharedEngine] playEffect:soundEffectName];//Play sound effect.
// [_bear runAction:_walkAction];
// }];

After that, any function to unset TouchBlock?

Owner

m1entus commented Dec 27, 2012

_bear.isTouchEnabled = NO

You have memory leak because you're using _bear inside block, you need to use weak bear inside block. Check block memory management.

__weak weakBear = _bear;

[weakBear stoAction:_walkAction];

yangboz commented Dec 27, 2012

Does this solution under ARC checked project?

Owner

m1entus commented Dec 27, 2012

[_bear setTouchBlock:^(CCTouchableSprite *sprite) {

__weak CCTouchableSprite *weakSprite = sprite;

[weakSprite stopAction:_walkAction];
[[SimpleAudioEngine sharedEngine] playEffect:soundEffectName];//Play sound effect.
[weakSprite runAction:_walkAction];

}];

Reference: http://stackoverflow.com/questions/4352561/retain-cycle-on-self-with-blocks

yangboz commented Dec 28, 2012

Test it again, no more memory leaked, thanks for your great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment