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

问题描述

我正在阅读网络和文档,但老实说,我听不懂。由于我是cocos2d-x的新手,所以我想更好地了解如何创建/保留对象以及应该如何释放它们(如果需要)。让我感到困惑的是我不太了解智能指针的用法。

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.

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

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);

那么自从我使用create()之后,应该将它发布到某个地方吗?在CCLayer的析构函数中?还是我对此没有任何关系?

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?

我了解C ++的基础知识,因此,如果我新建一个对象,我实际上必须在析构函数中或何时删除它我不再需要它了,但是cocos2dx对象呢?

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?

推荐答案

这里是东西,

类Ref 的对象或任何从其派生的类具有变量 _retainCount 代表对象的范围。

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

此外,还有一个 autorelease 池,类似于垃圾收集器在 java 中。添加到此 autorelease 池中的对象将在帧末尾删除。除非其 _retainCount!= 0

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

现在,当您创建 Ref的新对象时或使用create方法的派生类已经添加到 autorelease 池中,您无需 release 它或删除它在任何地方。正如您在下面的 Node 的create函数中所看到的。

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;
}

但是当您使用 new创建新对象时,您肯定需要删除使用完毕后。尽管不建议使用New来分配对象,但 cocos2d 类的对象。而是使用create。

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();

然后,

temp->retain();

//your work...

temp->release();

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

第二件事,

当对象被添加到 autorelease 池中,但是您需要增加它的范围就可以保留它,这会将它的保留计数增加一,然后您必须手动释放它,即减少

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.

第三件事,

无论何时添加子对象自动保留,但您不需要释放,而是如上所述将其从父级删除。

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.

官方文档位于下面的@link。

Official documentation is @link below.

[

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

08-16 03:35