My code has progressed to me encapsulating a sprite in my class. I notice that none of the examples ever bother to delete
or free
anymore although I did chance upon something like a safe_delete
macro/call the other day. As my sprite containing objects can frequently go out of scope and then be recreated is there anything I should be doing in my destructor?
Got off to a bad start with 48 of:
07-11 11:01:23.433: E/cocos2d-x assert(1027): MyProjPath\proj.android…/cocos2d/cocos/./2d/CCNode.cpp
function:~Node line:182
07-11 11:01:23.433: D/cocos2d-x debug info(1027): Assert
failed: Node still marked as running on node destruction! Was base class
onExit() called in derived class onExit() implementations?
all because didn’t call super class onExit()
…
But otherwise everything works great, no more memory errors.
Cocos2d-x use the retain/release model.
When you use the “create” methods, they are assumed (by convention) to do 2 things:
- Instantiate the object, with a retain count of 1 (basic behavior of cocos2d-x objects)
- autorelease the object, which decrease the retain count to 0 and increase the autorelease count to 1, meaning it will be released the next time objects are autoreleased (something similar from a high level perspective to a garbage collector)
In that case, your app should retain the object if you intend to use it later (or add it to a data store that retain children).
thanks @fraddow
owing to the traditionally sequential nature of programing i would imagine i would be using it later. how much later are we talking here? next frame or whenever memory is required to be released? i guess by data store you’re including an in memory array?
Consider it to be next frame (that’s a rough estimation, I don’t know the specifics)
By data store I mean cocos2d-x provided data structures, for example Array or Dictionary (the cocos2d-x ones).
C-style arrays (for example Sprite[] or using malloc/new) or std data structures (for example std::vector) are not going to retain anything for you, since they do not follow the retain/release model.