正确释放 SWIG 输入类型映射中的指针数组的方法?
2018-6-5 17:7:46
收藏:0
阅读:96
评论:1
大家好,我正在尝试使用 SWIG 包装以下函数。
static void readTable(int argc, t_atom *argv) { //在 Lua 中接受表格,例如 readTable({"ab",3});
for (int i=0; i<argc; ++i) {
if (argv[i].a_type == A_FLOAT)
printf("FLOAT : %g\n", argv[i].a_w.w_float);
else if (argv[i].a_type == A_SYMBOL)
printf("SYMBOL : %s\n", argv[i].a_w.w_symbol->s_name);
}
}
以下是我创建的类型映射。
%include "exception.i"
%typemap(in) (int argc, t_atom *argv)
{
if (!lua_istable(L, 1)) {
SWIG_exception(SWIG_RuntimeError, "argument mismatch: table expected");
}
lua_len(L, 1);
$1 = lua_tointeger(L, -1);
$2 = (t_atom *)getbytes($1 * sizeof(t_atom)); // 内部调用 calloc()
for (int i=0; i<$1; ++i) {
lua_pushinteger(L, i+1);
lua_gettable(L, 1);
if(lua_isnumber(L, -1)) {
$2[i].a_type = A_FLOAT;
$2[i].a_w.w_float = lua_tonumber(L, -1);
}
else if(lua_isstring(L, -1)) {
$2[i].a_type = A_SYMBOL;
$2[i].a_w.w_symbol = gensym(lua_tostring(L, -1));
}
}
freebytes($2, $1 * sizeof(t_atom)); // 内部调用 free()
}
如您所见,我使用 getbytes() 分配了一个数组的内存,该函数内部调用 calloc(),并使用 freebytes() 释放该内存,该函数内部调用 free()。
令人惊讶的是,当我在 Lua 中调用 readTable() 函数时,它可以正常运行而不会崩溃。
例如,在 Lua 中调用 test.readTable({3, "abc"}) 将打印以下结果。
FLOAT : 3
SYMBOL : abc
我的问题是
a) 当我在 SWIG 接口中释放内存时,readTable() 如何在没有崩溃的情况下打印结果?也许内存没有被正确释放?
b) 如果 a) 没有意义或者使用起来不安全,您会建议在函数 readTable() 中调用 freebytes() 来使其类似于以下代码吗?
static void readTable(int argc, t_atom *argv) {
for (int i=0; i<argc; ++i) {
if (argv[i].a_type == A_FLOAT)
post("FLOAT : %g", argv[i].a_w.w_float);
else if (argv[i].a_type == A_SYMBOL)
post("SYMBOL : %s", argv[i].a_w.w_symbol->s_name);
}
freebytes(argv, argc * sizeof(t_atom));
}
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

这正是
%typemap(freearg)应用的情况。%include "exception.i" %typemap(in) (int argc, t_atom *argv) { if (!lua_istable(L, $input)) { SWIG_exception(SWIG_RuntimeError, "argument mismatch: table expected"); } lua_len(L, $input); $1 = lua_tointeger(L, -1); $2 = (t_atom *)getbytes($1 * sizeof(t_atom)); // internally calls calloc() for (int i = 0; i < $1; ++i) { lua_pushinteger(L, i + 1); lua_gettable(L, $input); if (lua_isnumber(L, -1)) { $2[i].a_type = A_FLOAT; $2[i].a_w.w_float = lua_tonumber(L, -1); } else if (lua_isstring(L, -1)) { $2[i].a_type = A_SYMBOL; $2[i].a_w.w_symbol = gensym(lua_tostring(L, -1)); } else { SWIG_exception(SWIG_RuntimeError, "unhandled argument type"); } } } %typemap(freearg) (int argc, t_atom *argv) { freebytes($2, $1 * sizeof(t_atom)); // internally calls free() } static void readTable(const std::string &name, int argc, t_atom *argv);这是从 SWIG 3.0 生成的代码。
static int _wrap_readTable(lua_State* L) { int SWIG_arg = 0; std::string *arg1 = 0 ; int arg2 ; t_atom *arg3 = (t_atom *) 0 ; SWIG_check_num_args("readTable",2,2) if(!lua_isuserdata(L,1)) SWIG_fail_arg("readTable",1,"std::string const &"); if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_std__string,0))){ SWIG_fail_ptr("readTable",1,SWIGTYPE_p_std__string); } { if (!lua_istable(L, 2)) { SWIG_exception(SWIG_RuntimeError, "argument mismatch: table expected"); } lua_len(L, 2); arg2 = lua_tointeger(L, -1); arg3 = (t_atom *)getbytes(arg2 * sizeof(t_atom)); // internally calls calloc() for (int i = 0; i < arg2; ++i) { lua_pushinteger(L, i + 1); lua_gettable(L, 2); if (lua_isnumber(L, -1)) { arg3[i].a_type = A_FLOAT; arg3[i].a_w.w_float = lua_tonumber(L, -1); } else if (lua_isstring(L, -1)) { arg3[i].a_type = A_SYMBOL; arg3[i].a_w.w_symbol = gensym(lua_tostring(L, -1)); } else { SWIG_exception(SWIG_RuntimeError, "unhandled argument type"); } } } readTable((std::string const &)*arg1,arg2,arg3); { freebytes(arg3, arg2 * sizeof(t_atom)); // internally calls free() } return SWIG_arg; if(0) SWIG_fail; fail: { freebytes(arg3, arg2 * sizeof(t_atom)); // internally calls free() } lua_error(L); return SWIG_arg; }