簡體   English   中英

在SWIG輸入typemap中釋放指針數組的正確方法?

[英]Proper way to free a pointer array in SWIG input typemap?

嗨,我正在嘗試使用SWIG包裝以下函數。

static void readTable(int argc, t_atom *argv) { //accepts table in Lua e.g. 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)); //internally calls 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)); //internally calls free()
}

如您所見,我使用內部調用calloc() getbytes()為數組分配內存,並使用內部調用free() freebytes()釋放內存。

令人驚訝的是,當我在Lua中調用readTable()函數時,它可以正常工作而不會崩潰。

例如,在Lua中調用test.readTable({3, "abc"})打印以下結果。

FLOAT : 3
SYMBOL : abc

我的問題是

a)在SWIG界面中釋放內存時, readTable() )如何打印而不會崩潰? 也許沒有正確釋放內存?

b)如果a)沒有意義或使用不安全,您是否建議freebytes()freebytes()函數內調用readTable() ,使其類似於以下內容?

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));
    }

這正是%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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM