fuxi-asyncflow / asyncflow

an AI and gameplay editor for game development, written by c++ , suppport lua and python
MIT License
30 stars 11 forks source link

当Agent处于Destroying状态时,waiting_nodes_list存在被析构两次的情况 #17

Open bread2012 opened 10 months ago

bread2012 commented 10 months ago

void Manager::HandleEvent(AsyncEventBase& ev) { auto* agent = ev.GetAgent(); auto* waiting_nodes = agent->GetWaitNodes(ev.Id()); if (waiting_nodes == nullptr || waiting_nodes->IsEmpty()) return; current_event_ = &ev; waiting_nodes_ = waiting_nodes; agent->HandleEvent(ev, waiting_nodes); delete waiting_nodes; waiting_nodes_ = nullptr; current_event_ = nullptr; } 当Agent处于Destroying状态时:

  1. agent->GetWaitNodes(ev.Id());会正常返回
  2. agent->HandleEvent(ev, waiting_nodes); 判断自身处于Destroying其实不会被运行,不会在ev.Id()处new NodeList
  3. 然后delete waiting_nodes
  4. 在Agent析构的时候又析构了一次waiting_nodes_list[ev.Id()]里的元素
  5. 最终会造成进程crash
nanofrog commented 10 months ago

确实存在你说的这个问题,不过后面的提交中,我将反注册agent的逻辑修改为: deregister的时候,就将它从AgentManager中的移除了,这样在handlevent时,由于找不到agent,也就取不到waiting_nodes指针,就不会发生你上面所说的第一次析构的过程

bread2012 commented 10 months ago

确实存在你说的这个问题,不过后面的提交中,我将反注册agent的逻辑修改为: deregister的时候,就将它从AgentManager中的移除了,这样在handlevent时,由于找不到agent,也就取不到waiting_nodes指针,就不会发生你上面所说的第一次析构的过程

你们项目也是在用这个库吗?

nanofrog commented 10 months ago

我就是作者,昨天忘了换马甲了。有项目接入了,但还没有大规模使用,而且编辑器那边最近也在调整,所以目前来说项目还处于比较alpha的阶段

bread2012 commented 10 months ago

再问下,再asyncflow::lua::event函数内,有把参数存到LUA_REGISTRYINDEX table的操作,event_args[i] = luaL_ref(L, LUA_REGISTRYINDEX)。但是没有看到luaL_unref的操作呢,在delete event的时候只有helper.DecRef的操作,但是lua_manager.h的DecRef函数并没有做任何操作,有内存泄漏的风险呢

nanofrog commented 10 months ago

确实,当时忘了。你是在lua里面使用吗?我现在的改动都是在python的基础上在测试,lua那边的测试case都还没有准备。核心那部分应该都OK,但是导出接口这块可能没来得及更新。

bread2012 commented 10 months ago

对,我是在lua里使用的

bread2012 commented 10 months ago

NodeWaitAll:Run函数里 reset的时应该是iter->second=false吧?另外NodeWaitAll:Stop的时候也应该reset一次状态?

nanofrog commented 10 months ago

NodeWaitAll:Run函数里 reset的时应该是iter->second=false吧?另外NodeWaitAll:Stop的时候也应该reset一次状态?

你说得对,我马上改一下

nanofrog commented 10 months ago

已提交