Cocos2dx内存管理,如何使用析构函数和何时释放obejcts? [英] Cocos2dx memory management, how to use destructors and when to release obejcts?

查看:174
本文介绍了Cocos2dx内存管理,如何使用析构函数和何时释放obejcts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读网络和文档,但说实话,我不知道。因为我是新cocos2d-x我想更好地了解如何创建/保留对象和我应该做什么来释放它们(如果需要)。让我困惑的是使用智能指针,我不知道很好。



想象一下,在我的CCLayer(添加到CCScene)中,我添加了一个CCSprite,所以我这样做:

  this-> sprite = CCSprite :: create(mySprite.png); 
this-> addChild(sprite);

那么因为我使用了create()我应该释放它在某个地方?在CCLayer的析构函数可能?



我知道C ++的基础知识,所以如果我做新一个对象,我实际上必须在析构函数中删除它,或者我不需要它,但是cocos2dx对象呢?

解决方案

这是事情,



类Ref 的对象或从其派生的任何类都有一个变量 _retainCount 它表示对象的范围。



还有一个类似于垃圾收集器的 autorelease java 中。添加到此 autorelease 池的对象将在框架结束时删除。除非 _retainCount!= 0



现在当您创建 Ref 或派生类使用create方法,它已经添加到 autorelease 池,你不需要 release it或 delete 。您可以在下面的 Node 的创建函数中看到。

  * Node :: create()
{
Node * ret = new(std :: nothrow)Node();
if(ret&& ret-> init())
{
ret-> autorelease
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}

但是当使用'new'创建新对象时, c $ c> delete 它使用后就结束了。虽然不建议使用新建分配对象的 cocos2d 类。而是使用create。

  Node * temp = Node :: create 

然后,

  temp-> retain(); 

//你的工作...

temp-> release();

 c $ c> Node * temp = Node :: create(); 
Node * tempChild = Node :: create();
temp-> addChild(tempChild);
//你的工作...
temp-> removeFromParent();

第二件事,



被添加到 autorelease 池,但是你需要增加它的范围,你可以保留它,它增加其保留计数一,然后你必须手动释放它,即,递减



每当添加子对象时,它都是自动保留,但您不需要发布,而是如上所述将其从父级删除。



官方文档在下面是@link。



[ http://www.cocos2d-x.org/wiki/Reference_Count_and_AutoReleasePool_in_Cocos2d-x#Refrelease-retain-and-autorelease] [1]


I'm reading around the web and the documentation but to be honest, I don't get it. Since I'm new to cocos2d-x I would like to understand better how the objects are created/retained and what I'm supposed to do to release them (if required). The thing that confuses me is the usage of smart pointers that I don't know very well.

Imagine that in my CCLayer (added to the CCScene) I add a CCSprite, so i do:

this->sprite = CCSprite::create("mySprite.png");
this->addChild(sprite);

then since I've used create() I'm supposed to release it somewhere? in the destructor of the CCLayer maybe? or I have nothing to do about that?

I know the basics of C++ so if I do "new" an object I actually have to delete it in the destructor or when I don't need it anymore, but what about the cocos2dx objects?

解决方案

Here is the thing,

Object of the class Ref or any class derived from it has a variable _retainCount which represents the scope of the object.

Also there is an autorelease pool which is similar to the garbage collector in java. The object which is added to this autorelease pool will be deleted at the end of the frame. Unless its _retainCount!=0

Now when you create new object of Ref or derived class using the create method it is already added to the autorelease pool and you don't need to release it or delete it anywhere. As you can see in the create function of Node below.

Node * Node::create()
{
    Node * ret = new (std::nothrow) Node();
    if (ret && ret->init())
    {
        ret->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(ret);
    }
    return ret;
}

But when you create new object using 'new' you definitely need to delete it after its use is over. Though it is not recommended to use New to allocate the objects of cocos2d classes. Rather use create.

Node* temp=Node::create();

then,

temp->retain();

//your work...

temp->release();

or

Node* temp=Node::create();
Node* tempChild=Node::create();
temp->addChild(tempChild);
//your work...
temp->removeFromParent();

Second thing,

when your object is added to the autorelease pool but you need to increase its scope you could just retain it , this increments its retain count by one and then you have to manually release it i.e, decrement its retain count after its use is over.

Third Thing,

Whenever you add child your object it is retained automatically but you don't need to release it rather you remove it from the parent as mentioned above.

Official documentation is @link below.

[http://www.cocos2d-x.org/wiki/Reference_Count_and_AutoReleasePool_in_Cocos2d-x#Refrelease-retain-and-autorelease][1]

这篇关于Cocos2dx内存管理,如何使用析构函数和何时释放obejcts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆