Prevent the C++ part of PyQt objects being deleted while the Python component is still accessible.

This commit is contained in:
AnyOldName3
2018-05-02 00:19:29 +01:00
parent 902024e508
commit 94886104f3
+22 -2
View File
@@ -499,17 +499,33 @@ struct QClass_converters
{
struct QClass_to_PyQt
{
template <typename Q>
static typename std::enable_if_t<std::is_copy_constructible_v<Q>, T*> getSafeCopy(T *qClass)
{
return new T(*qClass);
}
template <typename Q>
static typename std::enable_if_t<!std::is_copy_constructible_v<Q>, T*> getSafeCopy(T *qClass)
{
return qClass;
}
static PyObject *convert(const T &object) {
const sipTypeDef *type = sipAPI()->api_find_type(MetaData<T>::className());
if (type == nullptr) {
return bpy::incref(Py_None);
}
PyObject *sipObj = sipAPI()->api_convert_from_type((void*)(&object), type, 0);
PyObject *sipObj = sipAPI()->api_convert_from_type((void*)getSafeCopy<T>((T*)&object), type, 0);
if (sipObj == nullptr) {
return bpy::incref(Py_None);
}
if (std::is_copy_constructible_v<T>)
// Ensure Python deletes the C++ component
sipAPI()->api_transfer_back(sipObj);
return bpy::incref(sipObj);
}
@@ -523,11 +539,15 @@ struct QClass_converters
return bpy::incref(Py_None);
}
PyObject *sipObj = sipAPI()->api_convert_from_type(object, type, 0);
PyObject *sipObj = sipAPI()->api_convert_from_type(getSafeCopy<T>(object), type, 0);
if (sipObj == nullptr) {
return bpy::incref(Py_None);
}
if (std::is_copy_constructible_v<T>)
// Ensure Python deletes the C++ component
sipAPI()->api_transfer_back(sipObj);
return bpy::incref(sipObj);
}