Skip to content

Commit

Permalink
print object status when access dead object
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongfq committed Aug 15, 2023
1 parent b191e5b commit 0d088cd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
38 changes: 10 additions & 28 deletions frameworks/cclua/src/olua/olua.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ static void *aux_toobj(lua_State *L, int idx, const char *cls, bool checked)
void *obj = olua_torawobj(L, idx);
if (olua_unlikely(!obj)) {
idx = lua_absindex(L, idx);

olua_printobj(L, "access dead object", idx);
luaL_error(L, "object '%s %p' survive from gc",
olua_typename(L, idx), lua_topointer(L, idx));
}
Expand Down Expand Up @@ -521,13 +521,10 @@ OLUA_API const char *olua_objstring(lua_State *L, int idx)
return env->buff;
}

OLUA_API void olua_print(lua_State *L, const char *fmt, ...)
OLUA_API void olua_print(lua_State *L, const char *str)
{
va_list ap;
va_start(ap, fmt);
olua_getglobal(L, "print");
lua_pushvfstring(L, fmt, ap);
va_end(ap);
olua_pushstring(L, str);
lua_pcall(L, 1, 0, 0);
}

Expand All @@ -540,10 +537,10 @@ OLUA_API void olua_printobj(lua_State *L, const char *tag, int idx)
snprintf(buf, sizeof(buf), "%s:%05"PRId64": %s {luaobj=%p,%s,%s}",
tag,
env->objcount,
olua_objstring(L, 2),
olua_objstring(L, idx),
luaobj,
tobitstr(luaobj->flags, bitstr),
luaobj->self ? olua_optfieldstring(L, 2, "name", "") : "0x0");
luaobj->self ? olua_optfieldstring(L, idx, "name", "") : "",
tobitstr(luaobj->flags, bitstr));
olua_print(L, buf);
}

Expand Down Expand Up @@ -1036,15 +1033,12 @@ static int cls_metamethod(lua_State *L)
}
return 0;
}

lua_replace(L, -2); // remove name
lua_insert(L, 1); // L: metamethod obj args...

lua_insert(L, 1);
if (!isgc) {
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}

if (!olua_isuserdata(L, 2)) {
// unnecessary call __gc on class table
return 0;
Expand All @@ -1054,17 +1048,17 @@ static int cls_metamethod(lua_State *L)
env->objcount--;
luaobj = olua_toluaobj(L, 2);

if (luaobj->flags & OLUA_FLAG_SKIP_GC) {
if (luaobj->flags & (OLUA_FLAG_SKIP_GC | OLUA_FLAG_DEL)) {
if (env->debug) {
olua_print(L, "skip gc for object: %s\n", olua_objstring(L, 2));
olua_printobj(L, "skip gc", 2);
}
return 0;
} else {
char buf[256];
char bitstr[OLUA_FLAGBITS];
size_t max_len = sizeof(buf);
size_t len = 0;

olua_setobjflag(L, 2, OLUA_FLAG_GC);
if (env->debug) {
const char *fmt = "lua gc:%05"PRId64": %s {luaobj=%p";
len = snprintf(buf, max_len, fmt,
Expand All @@ -1078,23 +1072,12 @@ static int cls_metamethod(lua_State *L)
len = strlen(buf);
}
}

if (luaobj->self) {
olua_pushobjtable(L);
lua_pushnil(L); // L: objtable nil
olua_rawsetp(L, -2, luaobj->self);
lua_pop(L, 1); // pop obj table
}

olua_setobjflag(L, 2, OLUA_FLAG_GC);
olua_pusherrorfunc(L);
lua_insert(L, 1);
lua_pcall(L, lua_gettop(L) - 2, LUA_MULTRET, 1);

if (!luaobj->self) {
luaobj->flags |= OLUA_FLAG_GC_DONE;
}

if (env->debug) {
if (len < max_len) {
snprintf(buf + len, max_len - len, ",%s}",
Expand All @@ -1103,7 +1086,6 @@ static int cls_metamethod(lua_State *L)
}
olua_print(L, buf);
}

return lua_gettop(L) - 1;
}
return 0;
Expand Down
17 changes: 9 additions & 8 deletions frameworks/cclua/src/olua/olua.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,14 @@ OLUA_API void *olua_torawobj(lua_State *L, int idx);
OLUA_API bool olua_getrawobj(lua_State *L, void *obj);

// object status flag
#define OLUA_FLAG_DEL 1 << 0 // delete by user
#define OLUA_FLAG_GC 1 << 1 // gc
#define OLUA_FLAG_GC_DONE 1 << 2 // gc done
#define OLUA_FLAG_SKIP_GC 1 << 3 // skip gc
#define OLUA_FLAG_IN_HEAP 1 << 4 // object in heap, gc: delete or free
#define OLUA_FLAG_IN_USERDATA 1 << 5 // object in userdata, gc: obj->~T()
#define OLUA_FLAG_IN_POOL 1 << 6 // object in pool
#define OLUA_FLAG_DEL 1 << 0 // delete by user
#define OLUA_FLAG_GC 1 << 1 // gc
#define OLUA_FLAG_GC_DONE 1 << 2 // gc done
#define OLUA_FLAG_SKIP_GC 1 << 3 // skip gc
#define OLUA_FLAG_IN_HEAP 1 << 4 // object in heap, gc: delete or free
#define OLUA_FLAG_IN_USERDATA 1 << 5 // object in userdata, gc: obj->~T()
#define OLUA_FLAG_IN_POOL 1 << 6 // object in pool
#define OLUA_FLAG_IN_SMARTPRT 1 << 7 // object in smartptr
OLUA_API void olua_setobjflag(lua_State *L, int idx, int flag);
OLUA_API bool olua_hasobjflag(lua_State *L, int idx, int flag);

Expand All @@ -222,7 +223,7 @@ OLUA_API void *olua_checkobj(lua_State *L, int idx, const char *cls);
OLUA_API void *olua_toobj(lua_State *L, int idx, const char *cls);
OLUA_API void olua_delobj(lua_State *L, void *obj);
OLUA_API const char *olua_objstring(lua_State *L, int idx);
OLUA_API void olua_print(lua_State *L, const char *fmt, ...);
OLUA_API void olua_print(lua_State *L, const char *str);
OLUA_API void olua_printobj(lua_State *L, const char *tag, int idx);
OLUA_API int olua_indexerror(lua_State *L);
OLUA_API int olua_newindexerror(lua_State *L);
Expand Down
1 change: 1 addition & 0 deletions frameworks/cclua/src/olua/olua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ int olua_push_object(lua_State *L, const std::shared_ptr<T> *value, const char *
}

olua_setobjflag(L, -2, OLUA_FLAG_SKIP_GC); // skip gc, managed by smart ptr
olua_setobjflag(L, -2, OLUA_FLAG_IN_SMARTPRT);
olua_pushobj<std::shared_ptr<T>>(L, new std::shared_ptr<T>(*value));
olua_addref(L, -3, OLUA_SMART_PRT, -1, OLUA_REF_ALONE);
lua_pop(L, 2); // pop nil and smartptr
Expand Down
2 changes: 1 addition & 1 deletion tools/lua-bindings/olua
Submodule olua updated 3 files
+6 −6 olua.c
+1 −0 olua.h
+1 −0 olua.hpp

0 comments on commit 0d088cd

Please sign in to comment.