有关可能在遍历中进行资源释放的资源释放过程设计
1.
存在Tick,可以把释放的资源记录。在下一帧开始或者该帧结束的时候进行统一的释放
class ActorManager
{
public:
void Tick()
{
m_arActor.Tick();
Clear();
}
void Free(Actor*)
{
m_arActorWaitForFree.push_back(Actor*);
}
void Clear()
{
for(m_arActorWaitForFree)
m_arActor.erase(m_arActorWaitForFree[i]);
m_arActorWaitForFree.clear();
}
private:
vector<Actor*> m_arActor;
vector<Actor*> m_arActorWaitForFree;
}
2.
没有Tick的,可以考虑设置回调现场。如果存在现场则,缓存在回调结束后进行释放。如果不存在现场则直接释放
class ActorManager
{
public:
void OnEvent()
{
m_bOnEvent++;
m_arActor.OnEvent();
Clear();
m_bOnEvent--;
}
void Free(Actor*)
{
m_arActorWaitForFree.push_back(Actor*);
if(!m_bOnEvent)
Clear()
}
void Clear()
{
for(m_arActorWaitForFree)
m_arActor.erase(m_arActorWaitForFree[i]);
m_arActorWaitForFree.clear();
}
private:
vector<Actor*> m_arActor;
vector<Actor*> m_arActorWaitForFree;
}
3.
在遍历前使用智能指针进行缓存。直接调用释放接口。回调结束智能指针自动释放
class ActorManager
{
public:
void OnEvent()
{
arTempOnEvent = m_arActor;
m_arActor.OnEvent();
arTempOnEvent.clear()
}
void Free(Actor*)
{
m_arActor.erase(Actor);
}
private:
vector<share_ptr<Actor>> m_arActor;
}
1.
存在Tick,可以把释放的资源记录。在下一帧开始或者该帧结束的时候进行统一的释放
class ActorManager
{
public:
void Tick()
{
m_arActor.Tick();
Clear();
}
void Free(Actor*)
{
m_arActorWaitForFree.push_back(Actor*);
}
void Clear()
{
for(m_arActorWaitForFree)
m_arActor.erase(m_arActorWaitForFree[i]);
m_arActorWaitForFree.clear();
}
private:
vector<Actor*> m_arActor;
vector<Actor*> m_arActorWaitForFree;
}
2.
没有Tick的,可以考虑设置回调现场。如果存在现场则,缓存在回调结束后进行释放。如果不存在现场则直接释放
class ActorManager
{
public:
void OnEvent()
{
m_bOnEvent++;
m_arActor.OnEvent();
Clear();
m_bOnEvent--;
}
void Free(Actor*)
{
m_arActorWaitForFree.push_back(Actor*);
if(!m_bOnEvent)
Clear()
}
void Clear()
{
for(m_arActorWaitForFree)
m_arActor.erase(m_arActorWaitForFree[i]);
m_arActorWaitForFree.clear();
}
private:
vector<Actor*> m_arActor;
vector<Actor*> m_arActorWaitForFree;
}
3.
在遍历前使用智能指针进行缓存。直接调用释放接口。回调结束智能指针自动释放
class ActorManager
{
public:
void OnEvent()
{
arTempOnEvent = m_arActor;
m_arActor.OnEvent();
arTempOnEvent.clear()
}
void Free(Actor*)
{
m_arActor.erase(Actor);
}
private:
vector<share_ptr<Actor>> m_arActor;
}