Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr_track_keepalive' into modus
Browse files Browse the repository at this point in the history
  • Loading branch information
dfb committed Jan 25, 2019
2 parents 50c784c + 3111c3c commit 9426a02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Source/UnrealEnginePython/Private/UEPyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,12 @@ static void ue_pyobject_dealloc(ue_PyUObject *self)
#if defined(UEPY_MEMORY_DEBUG)
UE_LOG(LogPython, Warning, TEXT("Destroying ue_PyUObject %p mapped to UObject %p"), self, self->ue_object);
#endif
FUnrealEnginePythonHouseKeeper *housekeeper = FUnrealEnginePythonHouseKeeper::Get();
if (self->owned)
{
FUnrealEnginePythonHouseKeeper::Get()->UntrackUObject(self->ue_object);
housekeeper->UntrackUObject(self->ue_object);
}
housekeeper->UnregisterPyUObject(self->ue_object);

if (self->auto_rooted && (self->ue_object && self->ue_object->IsValidLowLevel() && self->ue_object->IsRooted()))
{
Expand Down Expand Up @@ -1872,7 +1874,10 @@ ue_PyUObject *ue_get_python_uobject(UObject *ue_obj)
ue_py_object->py_dict = PyDict_New();
ue_py_object->owned = 0;

FUnrealEnginePythonHouseKeeper::Get()->RegisterPyUObject(ue_obj, ue_py_object);
FUnrealEnginePythonHouseKeeper *housekeeper = FUnrealEnginePythonHouseKeeper::Get();
housekeeper->RegisterPyUObject(ue_obj, ue_py_object);
Py_INCREF(ue_py_object); // this is needed only because the following decrefs it
housekeeper->TrackUObject(ue_obj);

#if defined(UEPY_MEMORY_DEBUG)
UE_LOG(LogPython, Warning, TEXT("CREATED UPyObject at %p for %p %s"), ue_py_object, ue_obj, *ue_obj->GetName());
Expand Down
15 changes: 14 additions & 1 deletion Source/UnrealEnginePython/Public/PythonHouseKeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
{
uint32 Garbaged = 0;
TArray<UObject *> BrokenList;
TArray<ue_PyUObject *> needDecRef;
for (auto &UObjectPyItem : UObjectPyMapping)
{
UObject *Object = UObjectPyItem.Key;
Expand All @@ -182,14 +183,26 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
BrokenList.Add(Object);
Garbaged++;
}
else if (Tracker.PyUObject->ob_base.ob_refcnt == 1)
{ // the tracker is the only thing keeping this alive, so release the last reference
#if defined(UEPY_MEMORY_DEBUG)
UE_LOG(LogPython, Warning, TEXT("Auto-decrefing UObject at %p %s"), Object, *Object->GetName());
#endif
needDecRef.Add(Tracker.PyUObject);
}
else
{
#if defined(UEPY_MEMORY_DEBUG)
UE_LOG(LogPython, Error, TEXT("UObject at %p %s is in use"), Object, *Object->GetName());
UE_LOG(LogPython, Error, TEXT("UObject at %p %s is in use, py ref count %d"), Object, *Object->GetName(), Tracker.PyUObject->ob_base.ob_refcnt);
#endif
}
}

for (ue_PyUObject *py_obj : needDecRef)
{
Py_DECREF(py_obj); // on the next GC run, this object will dealloc and unregister itself
}

for (UObject *Object : BrokenList)
{
FPythonUOjectTracker &Tracker = UObjectPyMapping[Object];
Expand Down

0 comments on commit 9426a02

Please sign in to comment.