修复App::free函数造成内存泄漏的bug

This commit is contained in:
Nomango 2017-10-05 11:28:13 +08:00
parent 34ab072229
commit 69ac58b46a
5 changed files with 40 additions and 13 deletions

View File

@ -383,6 +383,11 @@ void App::free()
}
// 删除所有定时器
Timer::clearAllTimers();
MouseMsg::clearAllListeners();
KeyMsg::clearAllListeners();
ActionManager::clearAllActions();
// 删除所有对象
FreePool::__clearAllObjects();
}
void App::destory()

View File

@ -9,14 +9,14 @@
/// 让其自动释放
// 释放池容器
static std::vector<Object*> pool;
static std::vector<Object*> s_vPool;
void FreePool::__flush()
{
// 创建迭代器
std::vector<Object*>::iterator iter;
// 循环遍历容器中的所有对象
for (iter = pool.begin(); iter != pool.end();)
for (iter = s_vPool.begin(); iter != s_vPool.end();)
{
// 若对象的引用的计数为 0
if ((*iter)->m_nRefCount == 0)
@ -24,7 +24,7 @@ void FreePool::__flush()
// 释放该对象
delete (*iter);
// 从释放池中删除该对象
iter = pool.erase(iter);
iter = s_vPool.erase(iter);
}
else
{
@ -35,12 +35,14 @@ void FreePool::__flush()
void FreePool::__add(Object * nptr)
{
#ifdef _DEBUG
for (auto o : pool)
{
assert(o != nptr); // 不得有重复的指针存在
}
#endif
pool.push_back(nptr); // 将一个对象放入释放池中
s_vPool.push_back(nptr); // 将一个对象放入释放池中
}
void FreePool::__clearAllObjects()
{
for (auto o : s_vPool)
{
delete o;
}
s_vPool.clear();
}

View File

@ -1,7 +1,8 @@
#include "..\easy2d.h"
Object::Object() :
m_nRefCount(0)
m_nRefCount(0),
m_bAutoRelease(false)
{
}
@ -21,5 +22,9 @@ void Object::release()
void Object::autoRelease()
{
FreePool::__add(this); // 将该对象放入释放池中
if (!m_bAutoRelease)
{
m_bAutoRelease = true;
FreePool::__add(this); // 将该对象放入释放池中
}
}

View File

@ -144,3 +144,13 @@ void ActionManager::stopAllActions()
action->stop();
}
}
void ActionManager::clearAllActions()
{
for (auto action : s_vActions)
{
action->autoRelease();
action->release();
}
s_vActions.clear();
}

View File

@ -206,6 +206,8 @@ private:
static void __flush();
// 将一个节点放入释放池
static void __add(Object * nptr);
// 删除所有节点
static void __clearAllObjects();
};
class Scene
@ -251,6 +253,7 @@ public:
protected:
int m_nRefCount;
bool m_bAutoRelease;
};
class MouseMsg
@ -1472,6 +1475,8 @@ public:
static void pauseAllActions();
// 停止当前存在的所有动作
static void stopAllActions();
// 删除当前存在的所有动作
static void clearAllActions();
private:
static void __exec();