Memory deallocation in Cocos2d-x

all static methods, just like “spriteWithXXXX”, “particleWithXXX”, “batchNodeWithXXXX”, not only start with “init”.

hi, has this problem be solved? I have to create a lot of stuff in a separate thread…
Could I turn off this autorelease feature?

Autorelease pool is not thread-safe now. If you create a lot of stuff in a separate thread, you’d better not to use autorelease feature.

The problem is that currently a lot of the classes do not provide a “init” function. Hence objects of those classes can only be created with the static “node” or “action” function (which put the created objects to the pool before returning.)

As I know, if the class provides a node or action function, it also provide a initxxx.

Sorry, seems you are right.

How could I disable auto release? What is the standard way to handle this situation?

Minggo Zhang wrote:

Autorelease pool is not thread-safe now. If you create a lot of stuff in a separate thread, you’d better not to use autorelease feature.

Basically, always create the object with “new” and “init”. Do not call “autorelease” after the objects are created.

Great, will try that. Thanks!

Leon Li wrote:

Basically, always create the object with “new” and “init”. Do not call “autorelease” after the objects are created.

Hey guys, apologize for bumping this thread up again.

But according to Walzer Wang’s first comment, I would like to know whether the current state of cocos2d-x that we can create multiple of NSAutoreleasePool object or not ?
Can we actually mix C*+ and Objective-C code together and get the most out of performance ? Such as using @autoreleasepool{} block in C*+ class.

By the way guys,
I found out that this kind of topic is very large and the source for learning is not entirely relating to cocos2d-x. So we need to study and learn from cocos2d and iOS platform itself.
I kept track of my own research links, posted it below. Hope it useful.

[Retain / Release thingy]


http://en.wikibooks.org/wiki/Programming_Mac_OS_X_with_Cocoa_for_Beginners/Some_Cocoa_essential_principles
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH

[ARC myths and not using it]
http://www.learn-cocos2d.com/2012/06/mythbusting-8-reasons-arc/#more-5130
http://www.learn-cocos2d.com/2011/11/everything-know-about-arc/#development-requirements
http://www.learn-cocos2d.com/2011/11/everything-know-about-arc/#property-keywords
http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html
http://longweekendmobile.com/2011/09/07/objc-automatic-reference-counting-in-xcode-explained/

one question:i have a level text file, it has some level informations. now i want to create a class to parse the information in it. do i have to create a class inherits from CCObject? or i just need to create a common class,inherits from nothing.if my class inherits from ccobject, do i have to implement constructor and init function both? what if my class inherits from nothing, is it ok not to implement init function?

GuangHai Lin wrote:

one question:i have a level text file, it has some level informations. now i want to create a class to parse the information in it. do i have to create a class inherits from CCObject? or i just need to create a common class,inherits from nothing.if my class inherits from ccobject, do i have to implement constructor and init function both? what if my class inherits from nothing, is it ok not to implement init function?

No, you don’t have to always inherits from CCObject class. I have my object pool class implemented without any involvement in inherit chain with cocos2d-x.
If you do inheriting, in case of CCObject, you don’t have to overwrite init function as there’s no base init function to overwrite or even you don’t have to create one if you don’t need. But you can create one and also with static create() function in order to do in normal way of 2-step initialization in cocos2d. And deallocate applies the same way similar to others, if you create object from init function manually then you have to retain and release yourself, but if you create from create() static function as you need to put autorelease() inside, you don’t have to do anything further.

It takes time to get familiar with the way cocos does things.

thanks a lot!

its an old thread, but may help someone, the “message loop” usually referred here most likely is the one delta call, so its not the end of the method or scope when they mention end of message loop, else all the create method will stop working as intended, and that is also the reason, why you can keep passing the object back to caller function and caller function then adds it to parent. I think in the document it should be cleared, its a wonderful engine but every time you need know more you have to digg into the code to understand.

can any body tell me that how memory deallocation take place when scene replace in cocos2d-x 3.2 + version … i m facing some serious issues with this , please help

Hi,
I intend to create my own classes to implement an Finite State machine (which won’t be derived from Cocos)

However, I am not sure where should I deallocate the constructed object/s. If the app crashes while running on a phone, how should the de-allocation be handled? Won’t memory leak when the app crashes ?

or where is all the exit code being called? any particular block?

I am new to Cocos2d-x, hence i am unsure how things work here.

Cheers,
JNSVS