Skip to content

Commit

Permalink
Fix segfault when Lua calls into a finalised script-bound object (#249)
Browse files Browse the repository at this point in the history
* Fix segfault when Lua calls into a finalised script-bound object

* Null out the *P<> instead of removing the userdata
  • Loading branch information
GinjaNinja32 authored Sep 10, 2024
1 parent 7b4ceb8 commit c78418a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/scriptInterfaceMagic.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ struct convert<T*>
luaL_argerror(L, idx-1, msg);
return;
}
if (*p == NULL)
{
ptr = NULL;
return;
}
ptr = dynamic_cast<T*>(***p);
//printf("ObjParam: %p\n", ptr);
}
Expand Down Expand Up @@ -165,6 +170,13 @@ struct convert<P<T>>
luaL_argerror(L, idx-1, msg);
return;
}
if (*p == NULL)
{
ptr = NULL;
const char* msg = lua_pushliteral(L, "Object expected, got destroyed object");
luaL_argerror(L, idx-1, msg);
return;
}
ptr = **p;
//printf("ObjParam: %p\n", ptr);
}
Expand Down Expand Up @@ -529,8 +541,10 @@ template<class T> class scriptBindObject
if (lua_isuserdata(L, -1)) //When a subclass is destroyed, it's metatable might call the __gc function on it's sub-metatable. So we can get nil values here, ignore that.
{
PT* p = static_cast< PT* >(lua_touserdata(L, -1));
if (*p)
if (*p) {
delete *p;
*p = nullptr;
}
}
lua_pop(L, 1);
return 0;
Expand Down

0 comments on commit c78418a

Please sign in to comment.