开发者

Scenes not completly released in Cocos2d iPhone application - debugging

I have an iPhone application in Cocos2d that sometimes crashes on actual device, due to memory problems.

What I have so far found out is that the scenes, when switched, aren't fully released - the [retainCount] for them is somewhat about 4-10 :)

The dealloc method never gets called, and then I assume, when I switch the scenes a few times, the memory issue shows up.

I'm wondering - where should I relese the scene? Since it has number of children, I suppose I should be doing a cleanup-removal of them. But it turns out that removing all children from the layer doesn't decrease the retain count of it. I added such a piece of code to my cleanup method:

- (void) cleanup {
    while ([self.children count] > 0) {
        CCLOG(@"child: %d - %@  rc: %d", 0, [self.children objectAtIndex:0], [[self.children objectAtIndex:0] retainCount]);
        [self removeChild:[self.children objectAtIndex:0] cleanup:YES];
    }
    [super cleanup];
}

开发者_Go百科But then the [self retainCount] method still returns a number greater then 1 or 0, and my dealloc doesn't get called.

Is there something I should be doing in order to release those children properly? If I add my own subclass of CCSprite as child, should I do something specific in that class' release or dealloc method, other then just calling it's [super] method?


Do not call retainCount

retainCount is useless, as you've discovered, when dealing with complex frameworks. There are any number of internal implementation details that could cause the retain count to be an unexpected value at any given time without indicating a bug.

You should release the scene to balance however many times you retained the scene, no more and no less.

If you release it more times than you retained it, you're app will likely crash whenever you [potentially accidentally] fix the real problem.

In general, when dealing with a hierarchy of items like views, layers, or sprites, you remove the root view/layer/sprite and that removal takes care of tearing down the hierarchy (including releasing as needed).

That assumes that you haven't retained anything in the hierarchy. If you have, then you need to also release those references when the root is removed and released.


Usually you don't have to release your children by yourself. How do you add your child?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜