Question

I'm used to work with Objective-C and Cocos2d so here is something that confuses me.

I do want to save some custom objects into a bidimensional array, so I'm doing like this, in the header of the class where I want the array I do:

public:

SomeClass *matrix[6][6];

and in the .cpp in the init method I do:

this->matrix[0][0] = SomeClass::create();

if I do this, the constructor, init and then the destructor are called in this order... Obviously the thing that confuses me is the destructor call.

If i do:

this->matrix[0][0] = new SomeClass;

SomeClass is a sublass of CCNode.

the constructor is called and this is fine... but since I do want to use the init method with create, how should I do? I don't know why with the create method it calls the destructor immediately when the method return.

I don't understand really what is going on with the init method and with the Create_func, since I'm new to Cocos2d-x this is something I would like to understand better.

this is the create func I'm using:

#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new __TYPE__(); \
    if (pRet && pRet->init()) \
    { \
       pRet->autorelease(); \
       return pRet; \
     } \
     else \
     { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
      } \
}
Was it helpful?

Solution 2

just use retain()..

SomeClass *newObject=SomeClass::create();
newObject->retain();
this->matrix[0][0] = newObject;

and then you need to release the objects manually later before deleting this array otherwise a memory leak may happen.

OTHER TIPS

So first of all, let me explain how cocos2d-x memory management work.

Since cocos2d-x is related to cocos2d, the cocos2d-x team willing to using retain/release stuff like in object-c.

Obviously, a CCObject will be hold in memory only if his retain value is larger than 0.

Once you call the function release();, the retain value of this object will minus 1, and if it reached 0, the destructor will be called.

Finally, the autorelase() function. What is it, for cocos2d-x, the core thread hold a CCArray in CCPoolManager::sharedPoolManager(). In each frame, all the object in this array will call release() once, and will be removed if the retain value == 0.

So let's come in this case.

I believe that the autorelease() is the reason why make you confused. When an object is created by new ClassName(), the retain == 1; But if it is called autorelease(), it still remain 1, but will be 0 in the next frame.

That's why when you use this->matrix[0][0] = SomeClass::create(); the constructor, init and destructor will be called one by one, but remember, the destructor is called in the next frame not immediately after the init().

So how to make these correct, there's many way to do this. Most easy way is : this->matrix[0][0] = SomeClass::create(); this->matrix[0][0]->retain();

Besides that, I strongly recommend that using CC_SYNTHESIZE_RETAIN(varType, varName, funName) to define a value instead of ClassName* valueName in the headder file.

And use set##funName(varType var) to set it instead of using valueName = ClassName::create().

For more information, you can have a read on CC_SYNTHESIZE_RETAIN marco.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top