PySide Bugzilla Closed for New Bugs

PySide is now a Qt Add-on and uses the Qt Project's JIRA Bug Tracker instead of this Bugzilla instance. This Bugzilla is left for reference purposes.

Bug 620 - QAbstractItemModel.createIndex(int,int,PyObject*) does not increment refcount
: QAbstractItemModel.createIndex(int,int,PyObject*) does not increment refcount
Status: CLOSED DUPLICATE of bug 618
Product: PySide
Classification: Unclassified
Component: QtCore
: HEAD
: All All
: P3 normal
Assigned To: renato filho
:
:
:
  Show dependency treegraph
 
Reported: 2011-01-17 16:10 EET by Sebastian Thiel
Modified: 2011-01-21 15:44 EET (History)
10 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Thiel 2011-01-17 16:10:38 EET
When using the QAbstractItemMode.createIndex(int, int, PyObject*) method from
python with values only available on the stack, the ref-count will drop to zero
after the index was created and the caller returns. This will deallocate the
memory associated with the object as its refcount drops to zero, although the
pointer can still be accessed using the internalPointer() method of the
respective index, causing a crash when trying to use it.

To me it seems there would be two spots to fix this. The first one is to insert
custom code to manually increment the ref into typesystem_core.xml

<add-function signature="createIndex(int,int,PyObject*)const"
return-type="QModelIndex">
        <inject-code class="target" position="beginning">
         Py_XINCREF(%PYARG_3);
        %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%CPPSELF.%FUNCTION_NAME(%1,
%2, %PYARG_3));
        </inject-code>
    </add-function>

The second spot would be to install a new handler in p_dealloc for the
deallocation of the wrapper's PyObject reference, which currently only uses a
default handler as it seems.

Another problem to overcome would be to store information about whether the
QModelIndex instance really stores a PyObject, as the respective Cpp object
only has one member for this, and casts it accordingly. Because of this, the
wrapper needs to keep track of whether an actual PyObject is present to prevent
casting an index, accessed through internalId, into an invalid pointer.

If the proposed fix is not usable (and to be honest I didn't test it as I am
not properly setup to do this) it would be appreciated to add this shortcoming
to the pitfalls area of the documentation and to the PySide docs of
QAbstractItemModel to help others to bypass the issue right away.

Thank You, 
Sebastian
Comment 1 Farsmo 2011-01-17 20:42:42 EET
Coincidentally we reported the same bug less than one hour apart :-)

*** This bug has been marked as a duplicate of bug 618 ***
Comment 2 renato filho 2011-01-21 15:44:51 EET
release beta4