Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QgsDataItem::createChildren(): fix SIP generated C++ method
The SIP_FACTORY annotation didn't do the job for a Python implementation of createChildren(). We must write a custom VirtualCatcherCode to make sure that if the returned QgsDataItem elements are Python objects they aren't killed too early. Fixes the new added test, which uses to crash before the fix, and corresponds to the use case of a plugin defining its own data items. Has been checked not to cause memory leaks. The change of SIP_FACTORY to SIP_TRANSFERBACK is to avoid a memory leak when Python calls a C++ createChildren() implementation. Was found by making test_provider_ogr.py::testDataItems() loop on its call to createChildren() (cherry picked from commit e55107f)
- Loading branch information
1 parent
e56c50d
commit df4a0fb
Showing
4 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsDataItem | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
__author__ = 'Even Rouault' | ||
__date__ = '14/11/2020 late in the night' | ||
__copyright__ = 'Copyright 2020, The QGIS Project' | ||
|
||
from qgis.PyQt.QtCore import QEventLoop | ||
from qgis.core import QgsDataCollectionItem, QgsLayerItem | ||
|
||
from qgis.testing import start_app, unittest | ||
|
||
app = start_app() | ||
|
||
|
||
class PyQgsLayerItem(QgsLayerItem): | ||
|
||
def __del__(self): | ||
self.tabSetDestroyedFlag[0] = True | ||
|
||
|
||
class PyQgsDataConnectionItem(QgsDataCollectionItem): | ||
|
||
def createChildren(self): | ||
children = [] | ||
|
||
# Add a Python object as child | ||
pyQgsLayerItem = PyQgsLayerItem(None, "name", "", "uri", QgsLayerItem.Vector, "my_provider") | ||
pyQgsLayerItem.tabSetDestroyedFlag = self.tabSetDestroyedFlag | ||
children.append(pyQgsLayerItem) | ||
|
||
# Add a C++ object as child | ||
children.append(QgsLayerItem(None, "name2", "", "uri", QgsLayerItem.Vector, "my_provider")) | ||
|
||
return children | ||
|
||
|
||
class TestQgsDataItem(unittest.TestCase): | ||
|
||
def testPythonCreateChildrenCalledFromCplusplus(self): | ||
""" test createChildren() method implemented in Python, called from C++ """ | ||
|
||
loop = QEventLoop() | ||
NUM_ITERS = 10 # set more to detect memory leaks | ||
for i in range(NUM_ITERS): | ||
tabSetDestroyedFlag = [False] | ||
|
||
item = PyQgsDataConnectionItem(None, "name", "", "my_provider") | ||
item.tabSetDestroyedFlag = tabSetDestroyedFlag | ||
|
||
# Causes PyQgsDataConnectionItem.createChildren() to be called | ||
item.populate() | ||
|
||
# wait for populate() to have done its job | ||
item.stateChanged.connect(loop.quit) | ||
loop.exec_() | ||
|
||
# Python object PyQgsLayerItem should still be alive | ||
self.assertFalse(tabSetDestroyedFlag[0]) | ||
|
||
self.assertEqual(len(item.children()), 2) | ||
self.assertEqual(item.children()[0].name(), "name") | ||
self.assertEqual(item.children()[1].name(), "name2") | ||
|
||
# Delete the object and make sure all deferred deletions are processed | ||
item.destroyed.connect(loop.quit) | ||
item.deleteLater() | ||
loop.exec_() | ||
|
||
# Check that the PyQgsLayerItem Python object is now destroyed | ||
self.assertTrue(tabSetDestroyedFlag[0]) | ||
tabSetDestroyedFlag[0] = False | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |