lua: Correct refcount handling in get_frame

When destroying a frame handle, the previous logic would copy the
shared_ptr in the userdata and then free it twice (once explicitly
and once at the end of the function), which is actually UB, even
if if worked fine so far. This commit now ensures that it's the
actual userdata's shared_ptr that's freed in the gc function.
This commit is contained in:
arch1t3cht 2024-02-17 02:13:43 +01:00
parent 48855787a1
commit c11f17b9e6
1 changed files with 8 additions and 8 deletions

View File

@ -201,25 +201,25 @@ namespace {
}
}
std::shared_ptr<VideoFrame> check_VideoFrame(lua_State *L) {
std::shared_ptr<VideoFrame> *check_VideoFrame(lua_State *L) {
auto framePtr = static_cast<std::shared_ptr<VideoFrame>*>(luaL_checkudata(L, 1, "VideoFrame"));
return *framePtr;
return framePtr;
}
int FrameWidth(lua_State *L) {
std::shared_ptr<VideoFrame> frame = check_VideoFrame(L);
std::shared_ptr<VideoFrame> frame = *check_VideoFrame(L);
push_value(L, frame->width);
return 1;
}
int FrameHeight(lua_State *L) {
std::shared_ptr<VideoFrame> frame = check_VideoFrame(L);
std::shared_ptr<VideoFrame> frame = *check_VideoFrame(L);
push_value(L, frame->height);
return 1;
}
int FramePixel(lua_State *L) {
std::shared_ptr<VideoFrame> frame = check_VideoFrame(L);
std::shared_ptr<VideoFrame> frame = *check_VideoFrame(L);
size_t x = lua_tointeger(L, -2);
size_t y = lua_tointeger(L, -1);
lua_pop(L, 2);
@ -239,7 +239,7 @@ namespace {
}
int FramePixelFormatted(lua_State *L) {
std::shared_ptr<VideoFrame> frame = check_VideoFrame(L);
std::shared_ptr<VideoFrame> frame = *check_VideoFrame(L);
size_t x = lua_tointeger(L, -2);
size_t y = lua_tointeger(L, -1);
lua_pop(L, 2);
@ -259,8 +259,8 @@ namespace {
}
int FrameDestroy(lua_State *L) {
std::shared_ptr<VideoFrame> frame = check_VideoFrame(L);
frame.~shared_ptr<VideoFrame>();
std::shared_ptr<VideoFrame> *frame = check_VideoFrame(L);
frame->~shared_ptr<VideoFrame>();
return 0;
}