大佬教程收集整理的这篇文章主要介绍了cocos2dx的内存管理机制,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
必须尊重作者:
版权声明:本文由(小塔)原创,转载请保留文章出处!
本文链接:
今天看了一下cocos2dx的内存管理机制,有些地方不太好理解搞了挺长的时间,现在感觉自己理解的差不多了,赶快写下自己的思路和互联网的广大朋友分享,如果你发现有错误的地方或者不理解的地方欢迎指正!首先我们必须说一下c++中变量的内存空间的分配问题,我们在c++中写一个类,可以在栈上分配内存空间也可以使用New在堆上分配内存空间,如果类对象是在栈上分配的内存空间,这个内存空间的管理就不是我们的事了,但如果是在堆上分配的内存空间,当然需要我们来手动的delete了!cocos2dx采用的是在堆上分配内存空间,想想看你在写程序的时候对于cocos2dx中的类是不是大多数都是通过工厂方法获得的一个指针,你见过在栈上分配内存空间的情况吗?所以问题来了,既然在堆上分配内存空间,那么如何管理这个内存空间,什么时候应该释放就是个问题了!在程序中,当我们创建了一个对象的时候,这块内存空间经常是被不同的对象引用,如果删除的早了,有对象还在引用这块内存空间那么程序必然要崩溃!所以cocos2dx引入了引用计数这个内存管理机制。
当我们在堆上分配一块内存空间的时候,这个对象的引用计数就是1,当有对象要引用这块内存空间的时候,这个引用计数就增加1,当有对象不再引用这块内存的时候引用计数就减1,当这个引用计数减为0的时候就使用delete删除掉这块内存,这样就做到了当有对象引用的时候会正常的访问这块内存,引用完毕也可以正常的回收。先来看一下如下的代码,有关引用计数的问题就会很清楚了!
1 |
//对象创建的时候引用计数被设置为1,这个是在它的构造函数中完成的,它会先调用父类CCOjbect的构造函数 |
2 |
//CCObject的构造函数如下所示 |
5 | CCSprite * sprite = new CCSprite(); |
7 | CCLog("retain count:%d",sprite->retainCount()); |
15 | 16 | 17 | //当我们调用autorelease方法的时候会调用这段代码 |
19 | //调用autorelease方法的时候对象会被放到自动回收池中,这个自动回收池在每帧结束的时候会调用一次对象的release方法 |
21 | :rgb(108,sprite->retainCount()); |
上边的代码有一处不是很好的理解,就是大家经常说的自动回收机制,也就是上边的autorelease方法,这个方法到底为我们做了什么,有什么作用,我们需要好好的搞清楚!首先需要澄清的一个概念就是帧,我们经常的说每秒多少多少帧,其实这个帧需要多少时间不是固定的,这个需要看每帧我们需要做多少事情,如果没一帧我们需要渲染很多的东西,那这一帧执行的时间当然就会很长的,游戏显得就会很卡,这个时候每秒的帧率就会下降的,所以不是时间决定的帧率,而是帧影响的时间!这个自动回收池就是在每帧结束的时候起作用的,在游戏的每一帧都会有一个大的循环,在一帧开始之前,系统建立了一个内存回收池,在这一帧的过程中,当我们调用了autorelease方法以后,我们的对象就会放到这个内存回收池中,当一帧结束的时候这个内存回收池就会释放掉,这个时候在内存回收池中的对象就会被release一下,也就是说引用计数就会减一,如果这个时候引用计数为0,就会删除对象了。如果引用计数不为0的话对象是不会被删除的,下一帧开始的时候系统又会创建一个内存回收池,这个时候在上一次添加的对象这个时候是不会重新添加到这个内存回收池中的,在这个内存回收池中的对象是你在这一帧中调用了autorelease函数的对象。好了,内存回收池我觉的我已经说清楚了。下面我们来看一下,通常我们的代码都是怎么写的,来看看这个自动回收机制怎么就做到自动回收了!
number" style="margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:top!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:3em!important; line-height:1.1em!important; font-family:Consolas,sprite->retainCount());//retain 1 |
//retain 2 |
首先我们需要分析一下上边的代码,调用了create工厂方法以后,内部的实现是先new一个CCSprite的对象,这个时候引用计数加1,然后调用autorelease方法,将这个对象放到了自动回收池中,因为这一帧还没有结束,当然引用计数就还是1,所以打印的结果就是1,当我们调用addChild的时候,传入这个CCSprite对象,这个时候在当前层接受了这个对象以后会把它的引用计数加一,表明当前层正在使用这块内存空间,所以现在的retain就是2了。当这一帧结束的时候自动回收池会将对象的引用计数-1,所以现在就只有CCLayer在引用这个对象了,当CCLayer析构的时候,它会调用这个对象的release方法,这个时候当然就会删除了这个CCSprite对象了。所以什么是自动回收机制呢,自动就是在这一帧结束的时候将对象开始new的时候加的那个引用计数减掉,而让引擎中持有对象引用的其他类去管理这个对象,当持有者析构的时候就删除引用,引擎中的类负责retain和release,这个也算是自动吧!下面我们通过俩个例子来理解一下这个内存管理机制。
bool
HelloWorld::init()
( !CCLayer::init() )
} |
"CloseSELEcted.png", |
->m_sprite->retainCount());
22 | true23 |
24 | //按钮的响应事件 |
29 | } |
好了,运行程序我们会发现程序崩溃了,我们来分析一下。当我们创建了CCSprite对象的时候引用计数为1,并且该对象被放到了内存回收池中,在这一帧以后就会release对象一次,这个时候引用计数为0的对象当然就被释放了,我们按下按钮去调用这个对象,内存已经释放掉了,程序能不崩溃吗?下面我们来将代码改成如下的这样,看看效果。
这个时候运行起来程序,在按钮的函数调用的时候你会看到输出的引用计数都是1,因为我们已经手动的retain了一下这个对象,虽然自动回收池将它release了一下,但是它的引用计数任然为1。既然我们retain了,所以在层析构的时候记得要release啊。这样内存才不会泄露!还有一个就是CCArray的例子,下面来看一下。
bRet =
;
////////////////////////////////////////////////////////////////////////// |
// 父类初始化 |
addChild(bomb1,1); |
addChild(bomb2,monospace!important; border:0px!important; font-size:1em!important; padding:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; BACkground:none!important">addChild(bomb3,monospace!important; border:0px!important; font-size:1em!important; padding:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; BACkground:none!important">addChild(bomb4,monospace!important; border:0px!important; font-size:1em!important; padding:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; BACkground:none!important">addChild(bomb5,monospace!important; border:0px!important; font-size:1em!important; padding:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; BACkground:none!important">addChild(bomb6,monospace!important; border:0px!important; font-size:1em!important; padding:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; BACkground:none!important">m_pBombsDisplayed = CCArray::create(bomb1,bomb2,bomb3,bomb4,bomb5,bomb6,monospace!important; border:0px!important; font-size:1em!important; padding:0px!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; direction:ltr!important; display:inline!important; BACkground:none!important">->scheduleupdate(); |
31 | 32 | returnbRet; |
43 | 其实这个原理和上边的例子是差不多的,我们创建了一个CCArray的对象,这个时候同样是没有添加到其他的层中的,所以在这一帧结束的时候就会将它的引用计数减1变成0,所以,再次使用的时候肯定就会出错了!在使用COcos2dx的内存管理的时候如果我们是通过工厂的方法创建的,并且add到了其他的层中的时候这个时候我们多数是不用担心的,但是如果你是通过new的方法创建的CCObject子类的对象,这个时候记住要在析构的时候release这个对象,如果在使用过程中你retain了对象,同样记住要release。本人总结的是,当我们调用函数传递CCObject子类对象的时候,在接受的时候我们都应该去retain一下,这样代表的是我要引用这块内存空间,什么时候你不再引用这块内存空间了,就release一下。其他的各种问题当你遇到的时候就想一下它的原理,就会明白的。最后的最后欢迎广大网友给我留言,我们共同探讨cocos2dx的问题!
以下是自己@R_489_11255@的: @H_138_10@main函数中有这样一句:return Application::geTinstance()->run();直接跳到这部分代码看下,while循环下有一句:director->mainLoop(); @H_909_801@void DisplayLinkDirector::mainLoop() { if (_purgeDirectorInNextLoop) { _purgeDirectorInNextLoop = false; purgeDirector(); } else if (! _invalid) { drawScene(); // release the objects PoolManager::geTinstance()->getCurrentPool()->clear(); } }可以看到,如果当前场景有效的话,调用了两句,这里看 PoolManager::geTinstance()->getCurrentPool()->clear(),跳转到clear函数:
void AutoreleasePool::clear() { #if Defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0) _isClearing = true; #endif for (const auto &obj : _managedObjectArray) { obj->release(); } _managedObjectArray.clear(); #if Defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0) _isClearing = false; #endif }可以看到,在mainLoop函数中,也就是每一帧中,每次都对回收池中的对象进行了release操作,并且把当前的内存池清空,再看release都做了什么:
void Ref::release() { CCassERT(_referenceCount > 0,"reference count should greater than 0"); --_referenceCount; if (_referenceCount == 0) { #if Defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0) auto poolManager = PoolManager::geTinstance(); if (!poolManager->getCurrentPool()->isClearing() && poolManager->isObjecTinPools(this)) { // trigger an assert if the reference count is 0 but the Ref is still in autorelease pool. // This happens when 'autorelease/release' were not used in pairs with 'new/retain'. // // Wrong usage (1): // // auto obj = Node::create(); // Ref = 1,but it's an autorelease Ref which means it was in the autorelease pool. // obj->autorelease(); // Wrong: If you wish to invoke autorelease several times,you should retain `obj` first. // // Wrong usage (2): // // auto obj = Node::create(); // obj->release(); // Wrong: obj is an autorelease Ref,it will be released when clearing current pool. // // Correct usage (1): // // auto obj = Node::create(); // |- new Node(); // `new` is the pair of the `autorelease` of next line // |- autorelease(); // The pair of `new Node`. // // obj->retain(); // obj->autorelease(); // This `autorelease` is the pair of `retain` of previous line. // // Correct usage (2): // // auto obj = Node::create(); // obj->retain(); // obj->release(); // This `release` is the pair of `retain` of previous line. CCassERT(false,"The reference shouldn't be 0 because it is still in autorelease pool."); } #endif delete this; } }对引用计数值减1,当为0的时候,直接从内存中删除。 下面拿sprite来看下: Sprite* Sprite::create(const std::string& fileName) { Sprite *sprite = new Sprite(); //引用计数为1 if (sprite && sprite->initWithFile(fileName)) { sprite->autorelease();//并且交给回收池管理 return sprite; } CC_SAFE_deletE(spritE); return nullptr; }当进行addchild操作时,有一句this->insertChild(child,zOrder);,跟进去: void Node::insertChild(Node* child,int z) 再看pushBACk, void pushBACk(T object) { CCassERT(object != nullptr,"The object should not be nullptr"); _data.push_BACk( object ); object->retain(); }这里又retain了一次,引用计数为2了。在上面一帧结束的时候release就变成了1.这时只有他的parent对他拥有引用计数1.当parent析构时, void Node::removeFromParentAndCleanup(bool cleanup) { if (_parent != nullptr) { _parent->removeChild(this,cleanup); } } void Node::removeChild(Node* child,bool cleanup /* = true */) { // explicit nil handling if (_children.empty()) { return; } ssize_t index = _children.geTindex(child); if( index != CC_INVALID_INDEX ) this->detachChild( child,index,cleanup ); } void Node::detachChild(Node *child,ssize_t childIndex,bool doCleanup) { // IMPORTANT: // -1st do onExit // -2nd cleanup if (_running) { child->onExitTransitionDidStart(); child->onExit(); } #if CC_USE_PHYSICS if (child->_physicsBody != nullptr) { child->_physicsBody->removeFromWorld(); } #endif // If you don't do cleanup,the child's actions will not get removed and the // its scheduledSELEctors_ Dict will not get released! if (doCleanup) { child->cleanup(); } // set parent nil at the end child->setParent(nullptr); _children.erase(childIndeX); }
iterator erase(ssize_t indeX) { CCassERT(!_data.empty() && index >=0 && index < size(),"Invalid index!"); auto it = std::next( begin(),index ); (*it)->release(); return _data.erase(it); }进行了release操作,这时引用对数为0,最后设置他的父节点为空,并且从父节点子节点数组中删除。 大佬总结以上是大佬教程为你收集整理的cocos2dx的内存管理机制全部内容,希望文章能够帮你解决cocos2dx的内存管理机制所遇到的程序开发问题。 如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。 本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。 猜你在找的Cocos2d-x相关文章
|