@@ -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