diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-05-25 11:24:21 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-05-25 19:10:21 +0200 |
commit | a97698f0a5d011fd206db94cf73dba6353f39b29 (patch) | |
tree | eff3ba570d69b8ec505f27ee1ac1de8ab776dab2 | |
parent | 2b885c0fa6148b96ec88c0f6720bdffd5b4f2ef5 (diff) |
Fix crashes when using the QImage(uchar *) data constructors, take 2
The constructors expect a range of memory that remains valid
through the lifetime of the image and may also modify it.
Crashes occurred since apparently the Py_Buffer was released before
it.
To fix this, inject code to increase the reference count
and decrease it in the cleanup function.
Fixes: PYSIDE-1563
Change-Id: Ic4a47e0f458ed5b59b88813139102a9f1490a9df
Reviewed-by: Christian Tismer <tismer@stackless.com>
(cherry picked from commit 7079bc4ffc63dd42054014b4b78bff78abed962b)
-rw-r--r-- | sources/pyside2/PySide2/QtGui/typesystem_gui_common.xml | 2 | ||||
-rw-r--r-- | sources/pyside2/PySide2/glue/qtgui.cpp | 7 | ||||
-rw-r--r-- | sources/pyside2/PySide2/templates/gui_common.xml | 3 |
3 files changed, 11 insertions, 1 deletions
diff --git a/sources/pyside2/PySide2/QtGui/typesystem_gui_common.xml b/sources/pyside2/PySide2/QtGui/typesystem_gui_common.xml index 54f18e133..c2109cae7 100644 --- a/sources/pyside2/PySide2/QtGui/typesystem_gui_common.xml +++ b/sources/pyside2/PySide2/QtGui/typesystem_gui_common.xml @@ -768,6 +768,8 @@ <include file-name="QStringList" location="global"/> <include file-name="QMatrix" location="global"/> </extra-includes> + <inject-code class="native" position="beginning" + file="../glue/qtgui.cpp" snippet="qimage-decref-image-data"/> <modify-function signature="load(const QString&, const char*)" allow-thread="yes"/> <modify-function signature="load(QIODevice*,const char*)" allow-thread="yes"/> diff --git a/sources/pyside2/PySide2/glue/qtgui.cpp b/sources/pyside2/PySide2/glue/qtgui.cpp index d96e9f78f..e3e25ebb5 100644 --- a/sources/pyside2/PySide2/glue/qtgui.cpp +++ b/sources/pyside2/PySide2/glue/qtgui.cpp @@ -172,6 +172,13 @@ for (int i = 0, i_max = %CPPSELF.count(); i < i_max; ++i){ %0 = new %TYPE(QPixmap::fromImage(%1)); // @snippet qpixmap +// @snippet qimage-decref-image-data +static void imageDecrefDataHandler(void *data) +{ + Py_DECREF(reinterpret_cast<PyObject *>(data)); +} +// @snippet qimage-decref-image-data + // @snippet qimage-constbits %PYARG_0 = Shiboken::Buffer::newObject(%CPPSELF.%FUNCTION_NAME(), %CPPSELF.byteCount()); // @snippet qimage-constbits diff --git a/sources/pyside2/PySide2/templates/gui_common.xml b/sources/pyside2/PySide2/templates/gui_common.xml index a139a5fe9..1a7406799 100644 --- a/sources/pyside2/PySide2/templates/gui_common.xml +++ b/sources/pyside2/PySide2/templates/gui_common.xml @@ -80,8 +80,9 @@ </template> <template name="qimage_buffer_constructor"> + Py_INCREF(%PYARG_1); auto ptr = reinterpret_cast<uchar*>(Shiboken::Buffer::getPointer(%PYARG_1)); - %0 = new %TYPE(ptr, %ARGS); + %0 = new %TYPE(ptr, %ARGS, imageDecrefDataHandler, %PYARG_1); </template> <template name="qcolor_repr"> |