I using lua coroutine , and in the lua thread , I create iup controls,  then 
when the lua thread end and gc,  game crash.

the patch can  avoid the crash , but I can't sure where to free the lua ref.

change the iuplua.c

void iuplua_plugstate(lua_State *L, Ihandle *ih)
{
        int ref;
        lua_pushthread(L);
        ref=lua_ref(L,LUA_REGISTRYINDEX);   /*keep ref for L, L maybe a thread*/
  IupSetAttribute(ih, "_IUPLUA_STATE_CONTEXT",(char *) L);
  IupSetAttribute(ih, "_IUPLUA_STATE_CONTEXT_REF",(char *) ref);
  printf("iuplua_plugstate %d\n",ref);
}

???? maybe free the ref on this function 'il_destroy_cb' ?????
static int il_destroy_cb(Ihandle* ih)
{
  /* called from IupDestroy. */
  char* sref = IupGetAttribute(ih, "_IUPLUA_WIDGET_TABLE_REF");
  if (sref)
  {
    lua_State *L = iuplua_getstate(ih);
    int ref = atoi(sref);

    /* removes the ihandle reference in the lua table */
    /* object.handle = nil */
    lua_rawgeti(L, LUA_REGISTRYINDEX, ref);  /* push object */
    lua_pushstring(L, "handle");
    lua_pushnil(L);
    lua_settable(L, -3);
    lua_pop(L,1);

    /* removes the association of the ihandle with the lua table */
    luaL_unref(L, LUA_REGISTRYINDEX, ref);  /* this is the complement of 
SetWidget */
    IupSetAttribute(ih, "_IUPLUA_WIDGET_TABLE_REF", NULL);
    IupSetCallback(ih, "LDESTROY_CB", NULL);
  }
  sref = IupGetAttribute(ih, "_IUPLUA_STATE_CONTEXT_REF");
  if (sref)
  {
          lua_State *L = iuplua_getstate(ih);
          int ref=(int)sref;
          lua_unref(L,ref);
        IupSetAttribute(ih, "_IUPLUA_STATE_CONTEXT_REF", NULL);
        printf("il_destroy_cb %d\n",ref);
  }

  return IUP_DEFAULT;
}
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Iup-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/iup-users

Reply via email to