zoukankan      html  css  js  c++  java
  • cocos2dx 内存管理学习

    Cocos2d-x是一套基于C++的引擎,C++的内存机制,如果采用new关键字声明一个对象而没有手动delete掉,那么申请的内存就不会被回收,进而造成内存泄露。
    
    autorelease 学习 
    
     
    
    cocos2d-x的导演类 有个mainLoop方法 引擎是用单一的线程来进行场景的绘制,通过不断调用mainLoop这个函数,这个函数除了进行场景的绘制,也会调用CCPoolManger函数的pop方法对自动管理的对象进行释放操作,pop方法会对CCAutoreleasePool堆栈栈顶的内存池进行操作,将池内的对象标记为非自动管理状态,并进行一次release操作,清除引用计数为1的对象,然后取出前一个入栈的内存池等待下一轮的释放
    
     
    
    void CCDisplayLinkDirector::mainLoop(void)
    
    { 
    
        pthread_mutex_lock(&scheduleSelectorMutex);
    
    //    CCLog("CCDirector pthread_mutex_lock");
    
        vector<</span>ThreadScheduleObject>::iterator itr;
    
        for (itr = pendingScheduleArray.begin(); itr!=pendingScheduleArray.end(); itr++) {
    
    //        CCLog("CCDirector pthread_mutex_lock 1");
    
            ThreadScheduleObject ob = *itr;
    
            (ob.target->*(ob.selector))(ob.object);
    
            ob.target->release();
    
            ob.object->release();
    
        }
    
        pendingScheduleArray.clear();
    
    //    CCLog("CCDirector pthread_mutex_preunlock");
    
        pthread_mutex_unlock(&scheduleSelectorMutex);
    
    //    CCLog("CCDirector pthread_mutex_unlock");
    
    if (m_bPurgeDirecotorInNextLoop)
    
    {
    
    purgeDirector();
    
            m_bPurgeDirecotorInNextLoop = false;
    
    }
    
    else if (! m_bInvalid)
    
      {
    
     drawScene();
    
     
    
     // release the objects
    
      CCPoolManager::getInstance()->pop();//这是调用了pop方法 上面时对场景的绘制
    
      }
    
    }
    
     
    
    void CCPoolManager::pop()
    
    {
    
        if (! m_pCurReleasePool)//m_pCurReleasePool内存池
    
        {
    
            return;
    
        }
    
     
    
      int nCount = m_pReleasePoolStack->count();//获得内存池的长度
    
     
    
    m_pCurReleasePool->clear();//清空内存池
    
     
    
      if(nCount > 1)
    
      {
    
    m_pReleasePoolStack->removeObjectAtIndex(nCount-1);
    
    m_pCurReleasePool = m_pReleasePoolStack->getObjectAtIndex(nCount - 2);
    
    }
    
     
    
     
    
    }
    
    
    自动释放有利有弊 他会可能造成内存过早释放cocos2d-x 能存管理学习
  • 相关阅读:
    多线程与MySQL(十)
    多进程与多线程(九)
    异常处理与网络编程(八)
    面向对象,绑定方法与异常处理(七)
    模块与对象(六)
    包与模块(五)
    迭代器与函数Python学习(四)
    函数与装饰器Python学习(三)
    数据库
    并发编程
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3019923.html
Copyright © 2011-2022 走看看