Search code examples
c++cocos2d-x

cocos2d-X call retain when calling a constructor


When we call a constructor on a object, do we need to call retain on the object ? I know when we call init method, it creates a auto-release object that needs to be retained ?

Kind Regards,


Solution

  • CCObject (or subclasses) allocation with new creates object with reference counter 1. When you call autorelease, counter remains at 1, but autoreleased objects with counter 1 will be destroyed at the end of update cycle (app initialization or current frame).

    In common cases, you create object with create static method, and follow common rules (retain only when we need to store object outside of container)

    If you create object with new call, object created already retained once. You can call autorelease on it, and follow common rules, or you can store it, and manually call release to destroy it.