如何防止 C API 注册表中的 Lua 回调被垃圾回收?
How to prevent a Lua callback, in the C API's registry, from being garbage collected?
我在我的C程序中使用了LUA 5.3回调系统。LUA脚本仅运行一次,以注册回调。我想防止回调为GC,而不会完全禁用GC。
回调代码:
LuaEventHandler* presentEvent = FindLuaEvent("present");
if (presentEvent != NULL) {
lua_rawgeti(lState, LUA_REGISTRYINDEX, presentEvent->luaFuncRef);
lua_pushvalue(lState, 1);
lua_pcall(lState, 0, 0, 0);
//presentEvent->luaFuncRef = luaL_ref(lState, LUA_REGISTRYINDEX);
}
回调添加代码:
LUACFUNCTION(RegisterCallbackLua) {
int ref = luaL_ref(lState, LUA_REGISTRYINDEX);
const char* name = luaL_checkstring(lState, 1);
RegisterLuaEvent(ref, name);
return 0;
}
LuaEventHandler* RegisterLuaEvent(int pLuaFuncRef, const char* pEventName) {
LuaEventHandler* eventHandler = new LuaEventHandler();
eventHandler->luaFuncRef = pLuaFuncRef;
eventHandler->eventName = pEventName;
printf("Callback registered: %sn", pEventName);
for (int i = 0; i < 64; i++) {
if (luaEventHandlerList[i] != 0)
continue;
luaEventHandlerList[i] = eventHandler;
break;
}
return eventHandler;
}
问题是不良内存管理。
luaeventhandlerlist应该是VEC,我直接引用LUA字符串,等等
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 用于在回调中调用解析器的设计模式
- 如何使用C++对象的成员函数作为 C 样式回调?
- Java从C++回调到C++回调
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- 如何防止 C API 注册表中的 Lua 回调被垃圾回收?
- 如何在 Lua 回调中检测可选的第一个参数
- Lua 使用元数据回调
- 如何链接到 Lua 中的回调函数,以便在重新加载脚本时更新回调
- 使用可变参数模板从 Lua 函数回调 (C api) 中提取参数
- C++和Lua-无保护错误(错误回调)?这怎么可能
- 为菜单系统实现lua回调
- lubridge回调(将lua函数传递给c++)
- 使用自定义数据作为函数参数调用lua回调
- 存储 Lua 回调函数