Skip to content

Commit

Permalink
fixed leak when calling UFUNCTIONs with table as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeioris committed Feb 24, 2020
1 parent 5590418 commit 0d57ff0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Source/LuaMachine/Private/LuaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,16 @@ int ULuaState::MetaTableFunction__call(lua_State* L)
void* Parameters = FMemory_Alloca(LuaCallContext->Function->ParmsSize);
FMemory::Memzero(Parameters, LuaCallContext->Function->ParmsSize);

for (TFieldIterator<UProperty> It(LuaCallContext->Function.Get()); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It)
{
UProperty* Prop = *It;
if (!Prop->HasAnyPropertyFlags(CPF_ZeroConstructor))
{
Prop->InitializeValue_InContainer(Parameters);
}
}


if (bImplicitSelf)
{
NArgs--;
Expand Down Expand Up @@ -984,6 +994,12 @@ int ULuaState::MetaTableFunction__call(lua_State* L)
}
}

for (TFieldIterator<UProperty> It(LuaCallContext->Function.Get()); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It)
{
It->DestroyValue_InContainer(Parameters);
}


if (ReturnedValues > 0)
return ReturnedValues;

Expand Down Expand Up @@ -1361,6 +1377,11 @@ void ULuaState::Len(int Index)
lua_len(L, Index);
}

int32 ULuaState::ILen(int Index)
{
return luaL_len(L, Index);
}

int32 ULuaState::ToInteger(int Index)
{
return lua_tointeger(L, Index);
Expand Down
2 changes: 2 additions & 0 deletions Source/LuaMachine/Public/LuaState.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ class LUAMACHINE_API ULuaState : public UObject

void Len(int Index);

int32 ILen(int Index);

void RawGetI(int Index, int N);
void RawSetI(int Index, int N);

Expand Down
6 changes: 4 additions & 2 deletions Source/LuaMachineEditor/Private/LuaMachineEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class SLuaMachineDebugger : public SCompoundWidget, public FGCObject
return;

SelectedLuaState->PushGlobalTable();
FLuaValue CurrentLuaTableOwner = SelectedLuaState->ToLuaValue(-1);
SelectedLuaState->PushNil(); // first key
while (SelectedLuaState->Next(-2))
{
Expand Down Expand Up @@ -212,7 +211,10 @@ class SLuaMachineDebugger : public SCompoundWidget, public FGCObject

if (LuaState->GetInternalLuaState())
{
DebugTextContext += FString::Printf(TEXT("%s at 0x%p (used memory: %dk) (top of the stack: %d) (uobject refs: %d) (tracked user data: %d)\n"), *LuaState->GetName(), LuaState, LuaState->GC(LUA_GCCOUNT), LuaState->GetTop(), Referencers.Num(), LuaState->TrackedLuaUserDataObjects.Num());
LuaState->PushRegistryTable();
int32 RegistrySize = LuaState->ILen(-1);
LuaState->Pop();
DebugTextContext += FString::Printf(TEXT("%s at 0x%p (used memory: %dk) (top of the stack: %d) (registry size: %d) (uobject refs: %d) (tracked user data: %d)\n"), *LuaState->GetName(), LuaState, LuaState->GC(LUA_GCCOUNT), LuaState->GetTop(), RegistrySize, Referencers.Num(), LuaState->TrackedLuaUserDataObjects.Num());
}
else
{
Expand Down

0 comments on commit 0d57ff0

Please sign in to comment.