Skip to content

Commit 948825c

Browse files
author
Roberto De Ioris
committed
improved python-owned object tracking
1 parent b61ddc2 commit 948825c

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

Source/UnrealEnginePython/Private/UEPyModule.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -1330,15 +1330,12 @@ static PyObject *ue_PyUObject_call(ue_PyUObject *self, PyObject *args, PyObject
13301330
PyTuple_SetItem(py_args, 2, py_name);
13311331
}
13321332
ue_PyUObject *ret = (ue_PyUObject *)py_unreal_engine_new_object(nullptr, py_args);
1333+
Py_DECREF(py_args);
13331334
if (!ret)
13341335
{
1335-
Py_DECREF(py_args);
13361336
return NULL;
13371337
}
1338-
// when new_object is called the reference counting is 2
1339-
Py_DECREF(ret);
1340-
ret->owned = 1;
1341-
1338+
// when new_object is called the reference counting is 2 and is registered in the GC
13421339
// UObject crated explicitely from python, will be managed by python...
13431340
FUnrealEnginePythonHouseKeeper::Get()->TrackUObject(ret->ue_object);
13441341

Source/UnrealEnginePython/Public/PythonHouseKeeper.h

+20-7
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
1616
{
1717
FWeakObjectPtr Owner;
1818
ue_PyUObject *PyUObject;
19+
bool bPythonOwned;
1920

2021
FPythonUOjectTracker(UObject *Object, ue_PyUObject *InPyUObject)
2122
{
2223
Owner = FWeakObjectPtr(Object);
2324
PyUObject = InPyUObject;
25+
bPythonOwned = false;
2426
}
2527
};
2628

@@ -110,6 +112,17 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
110112

111113
void TrackUObject(UObject *Object)
112114
{
115+
FPythonUOjectTracker *Tracker = UObjectPyMapping.Find(Object);
116+
if (!Tracker)
117+
{
118+
return;
119+
}
120+
if (Tracker->bPythonOwned)
121+
return;
122+
Tracker->bPythonOwned = true;
123+
// when a new ue_PyUObject spawns, it has a reference counting of two
124+
Py_DECREF(Tracker->PyUObject);
125+
Tracker->PyUObject->owned = 1;
113126
PythonTrackedObjects.Add(Object);
114127
}
115128

@@ -141,14 +154,14 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
141154
#if defined(UEPY_MEMORY_DEBUG)
142155
UE_LOG(LogPython, Warning, TEXT("DEFREF'ing UObject at %p (refcnt: %d)"), Object, Tracker->PyUObject->ob_base.ob_refcnt);
143156
#endif
144-
if (!Tracker->PyUObject->owned)
157+
if (!Tracker->bPythonOwned)
145158
Py_DECREF((PyObject *)Tracker->PyUObject);
146159
UnregisterPyUObject(Object);
147160
return nullptr;
148-
}
161+
}
149162

150163
return Tracker->PyUObject;
151-
}
164+
}
152165

153166
uint32 PyUObjectsGC()
154167
{
@@ -168,19 +181,19 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
168181
#endif
169182
BrokenList.Add(Object);
170183
Garbaged++;
171-
}
184+
}
172185
else
173186
{
174187
#if defined(UEPY_MEMORY_DEBUG)
175188
UE_LOG(LogPython, Error, TEXT("UObject at %p %s is in use"), Object, *Object->GetName());
176189
#endif
177-
}
190+
}
178191
}
179192

180193
for (UObject *Object : BrokenList)
181194
{
182195
FPythonUOjectTracker &Tracker = UObjectPyMapping[Object];
183-
if (!Tracker.PyUObject->owned)
196+
if (!Tracker.bPythonOwned)
184197
Py_DECREF((PyObject *)Tracker.PyUObject);
185198
UnregisterPyUObject(Object);
186199
}
@@ -223,7 +236,7 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
223236

224237
}
225238
return Garbaged;
226-
}
239+
}
227240

228241
UPythonDelegate *NewDelegate(UObject *Owner, PyObject *PyCallable, UFunction *Signature)
229242
{

0 commit comments

Comments
 (0)