Skip to content

Commit 2fbf288

Browse files
author
Roberto De Ioris
committed
attempt to fix #490
1 parent d9b8583 commit 2fbf288

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

Source/UnrealEnginePython/Private/PythonSmartDelegate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void FPythonSmartDelegate::PyFOnAssetPostImport(UFactory *factory, UObject *u_ob
6868
void FPythonSmartDelegate::PyFOnMainFrameCreationFinished(TSharedPtr<SWindow> InRootWindow, bool bIsNewProjectWindow)
6969
{
7070
FScopePythonGIL gil;
71-
PyObject *ret = PyObject_CallFunction(py_callable, (char *)"OO", py_ue_new_swidget<ue_PySWindow>(InRootWindow.ToSharedRef(), &ue_PySWindowType), bIsNewProjectWindow ? Py_True : Py_False);
71+
PyObject *ret = PyObject_CallFunction(py_callable, (char *)"NO", py_ue_new_swidget<ue_PySWindow>(InRootWindow.ToSharedRef(), &ue_PySWindowType), bIsNewProjectWindow ? Py_True : Py_False);
7272
if (!ret)
7373
{
7474
unreal_engine_py_log_error();

Source/UnrealEnginePython/Private/UEPyEditor.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,10 +1824,8 @@ PyObject *py_unreal_engine_editor_on_asset_post_import(PyObject * self, PyObject
18241824
if (!PyCallable_Check(py_callable))
18251825
return PyErr_Format(PyExc_Exception, "object is not a callable");
18261826

1827-
// will brutally leak
1828-
FPythonSmartDelegate *py_delegate = new FPythonSmartDelegate();
1829-
py_delegate->SetPyCallable(py_callable);
1830-
FEditorDelegates::OnAssetPostImport.AddRaw(py_delegate, &FPythonSmartDelegate::PyFOnAssetPostImport);
1827+
TSharedRef<FPythonSmartDelegate> py_delegate = FUnrealEnginePythonHouseKeeper::Get()->NewPythonSmartDelegate(py_callable);
1828+
FEditorDelegates::OnAssetPostImport.AddSP(py_delegate, &FPythonSmartDelegate::PyFOnAssetPostImport);
18311829
Py_RETURN_NONE;
18321830
}
18331831

@@ -1841,12 +1839,17 @@ PyObject *py_unreal_engine_on_main_frame_creation_finished(PyObject * self, PyOb
18411839

18421840
if (!PyCallable_Check(py_callable))
18431841
return PyErr_Format(PyExc_Exception, "object is not a callable");
1842+
/*
1843+
TSharedRef<FPythonSmartDelegate> py_delegate = FUnrealEnginePythonHouseKeeper::Get()->NewPythonSmartDelegate(py_callable);
1844+
IMainFrameModule& MainFrameModule = FModuleManager::LoadModuleChecked<IMainFrameModule>(TEXT("MainFrame"));
1845+
MainFrameModule.OnMainFrameCreationFinished().AddSP(py_delegate, &FPythonSmartDelegate::PyFOnMainFrameCreationFinished);
1846+
*/
18441847

1845-
// will brutally leak
18461848
FPythonSmartDelegate *py_delegate = new FPythonSmartDelegate();
18471849
py_delegate->SetPyCallable(py_callable);
18481850
IMainFrameModule& MainFrameModule = FModuleManager::LoadModuleChecked<IMainFrameModule>(TEXT("MainFrame"));
18491851
MainFrameModule.OnMainFrameCreationFinished().AddRaw(py_delegate, &FPythonSmartDelegate::PyFOnMainFrameCreationFinished);
1852+
18501853
Py_RETURN_NONE;
18511854
}
18521855

Source/UnrealEnginePython/Public/PythonHouseKeeper.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Slate/UEPySlateDelegate.h"
88
#include "Runtime/CoreUObject/Public/UObject/GCObject.h"
99
#include "PythonDelegate.h"
10+
#include "PythonSmartDelegate.h"
1011

1112
class FUnrealEnginePythonHouseKeeper : public FGCObject
1213
{
@@ -257,6 +258,16 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
257258
return Delegate;
258259
}
259260

261+
TSharedRef<FPythonSmartDelegate> NewPythonSmartDelegate(PyObject *PyCallable)
262+
{
263+
TSharedRef<FPythonSmartDelegate> Delegate = MakeShareable(new FPythonSmartDelegate());
264+
Delegate->SetPyCallable(PyCallable);
265+
266+
PyStaticSmartDelegatesTracker.Add(Delegate);
267+
268+
return Delegate;
269+
}
270+
260271
void TrackDeferredSlateDelegate(TSharedRef<FPythonSlateDelegate> Delegate, TSharedRef<SWidget> Owner)
261272
{
262273
FPythonSWidgetDelegateTracker Tracker(Delegate, Owner);
@@ -280,5 +291,7 @@ class FUnrealEnginePythonHouseKeeper : public FGCObject
280291
TArray<FPythonSWidgetDelegateTracker> PySlateDelegatesTracker;
281292
TArray<TSharedRef<FPythonSlateDelegate>> PyStaticSlateDelegatesTracker;
282293

294+
TArray<TSharedRef<FPythonSmartDelegate>> PyStaticSmartDelegatesTracker;
295+
283296
TArray<UObject *> PythonTrackedObjects;
284297
};

0 commit comments

Comments
 (0)