简体   繁体   中英

Cocos2D Scene Deallocation

When exactly is the dealloc method of a Cocos2D scene called? Right when another scene is loaded? Or after the scene has finished loading?
Additionally, what must be in the dealloc method if I plan on overwriting it?
Apparently, cocos2D automatically removes all nodes attached to a scene when the scene is deallocated. However, if I plan on overwriting it, specifically what methods must I call? So how can I manage my scene to make sure they are leak free, 100%? Thanks in advance!

Every object you allocate in your scene class which is not added to the scene as a child must be released explicitly.

A scene is released (and most of the times deallocated if you have not retained it somewhere else) when you replace it (with the replaceScene method)with another scene object or pop it from the scenes stack.

It is always advisable to look in the source code (which contains very helpful comments) and learn what exactly should be done if you customize your scene class.

CCScene's deallocator follows the same rules as any other object's. It will run when the reference count to CCScene reaches zero.

A scene by default doesn't care about another which is about to replace it, but if you're changing scene with a transition, there will be a period of time when two scenes exist at the same time. When the transition ends, the transition will release its reference to the first scene, which will probably be the last such reference, and the first scene will then be deallocated.

In your scene deallocator, put a CCLOG to see exactly when it's run. I put one in each to make sure.

In general, manually release any object which you created using a method starting with alloc , new or copy , whether or not it's a Cocos2D object or otherwise. Other creation methods, such as Cocos2D's node , do not require a manual release, unless you have chosen to retain the object manually yourself as well, which might be advisable if you are not going to add it as a child to another node straight away.

As you have said, adding a node as a child to another does not mean it needs an extra release ; Cocos2D will handle that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM