Gui: Use Py::SmartPtr instead of raw pointer

Using the raw pointer may result into a crash in case the Python
object is accessed by the interpreter after the NavigationStyle
has been destroyed.
This commit is contained in:
wmayer
2025-01-22 17:00:24 +01:00
committed by Ladislav Michl
parent 0ea02d60c1
commit 9e1d85ebd2
2 changed files with 10 additions and 9 deletions

View File

@@ -319,10 +319,10 @@ NavigationStyle::~NavigationStyle()
finalize();
delete this->animator;
if (pythonObject) {
if (!pythonObject.is(nullptr)) {
Base::PyGILStateLocker lock;
Py_DECREF(pythonObject);
pythonObject = nullptr;
Base::PyObjectBase* obj = static_cast<Base::PyObjectBase*>(pythonObject.ptr());
obj->setInvalid();
}
}
@@ -1914,11 +1914,11 @@ void NavigationStyle::openPopupMenu(const SbVec2s& position)
PyObject* NavigationStyle::getPyObject()
{
if (!pythonObject)
pythonObject = new NavigationStylePy(this);
Py_INCREF(pythonObject);
return pythonObject;
if (pythonObject.is(nullptr)) {
// ref counter is set to 1
pythonObject = Py::asObject(new NavigationStylePy(this));
}
return Py::new_reference_to(pythonObject);
}
// ----------------------------------------------------------------------------------

View File

@@ -37,6 +37,7 @@
#include <QEvent>
#include <Base/BaseClass.h>
#include <Base/SmartPtrPy.h>
#include <Gui/Namespace.h>
#include <FCGlobal.h>
#include <memory>
@@ -283,7 +284,7 @@ protected:
SbSphereSheetProjector * spinprojector;
//@}
PyObject* pythonObject;
Py::SmartPtr pythonObject;
private:
friend class NavigationAnimator;