Why create TMXTiledMap with "new" when you have a static "create"-method?

Why create TMXTiledMap with "new" when you have a static "create"-method?
0.0 0


I’ve noticed that most classes has a “create” method which creates a new instance with the “new” keyword and automatically initializes the object with the provided parameters. What I’m having trouble deciphering is that if that somewhere gets deleted from memory later. I know that “new” creates the instance in the dynamic memory and you have to be careful not to get memory leaks so I’m wondering if it’s necessary to keep track of the lifetime of the object and make sure to delete it when using “create”? To me it seems like the macro CC_SAFE_DELETE(pRet); in the source of TMXTiledMap takes care of it. Am I correct in assuming this?


I think in short if you use create then the magic happens AutoReleasePool with take care of it’s release don’t really need to worry about the delete
If you use new you’ll have to delete it later at some point

Problem is, what I said about autorelease is not true, and here is some detailed explanation


PS:CC_SAFE_DELETE only happens when something goes wrong, so it’s not really doing anything here :slight_smile:


For 2.x:

If you are using a Cocos2d-x object I would suggest don’t use new, use create() methods.

The documentation is great and you can also read about Cocos2d Autorelease pool on iPhone, theory is the same.

CC_SAFE_DELETE shouldn’t be necessary in most cases.

CC_SAFE_RELEASE_NULL is what I most often use.


Thanks for the clarification! I now understand :slight_smile: