https://issues.qgis.org/https://issues.qgis.org/favicon.ico2016-10-09T02:48:48ZQGIS Issue TrackingQGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=728952016-10-09T02:48:48ZVictor Olayavolayaf@gmail.com
<ul></ul><p>It's an important issue, no doubt, but i dont understand how a layer object can be missing that method... It's puzzling...</p>
<p>If that's the case, i guess models wont work, right? HAve you tested?</p> QGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=728972016-10-09T18:16:53ZMathieu Pellerin - nIRVnirvn.asia@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/download/10457/qgsvectorlayer_error.py">qgsvectorlayer_error.py</a><a href="/attachments/10457/qgsvectorlayer_error.py"><img alt="Magnifier" src="/images/magnifier.png" /></a> added</li><li><strong>Category</strong> changed from <i>Processing/Core</i> to <i>Python plugins</i></li></ul><p>Ok, this goes beyond processing, something is broken at the sip level. Here's a greatly narrowed steps to reproduce the issue:</p>
<strong>Steps to reproduce</strong>
<ol>
<li>Launch QGIS and create a blank project</li>
<li>Toggle the QGIS python console on, and load the attached script (qgsvectorlayer_error.py) in the editor</li>
<li>Replace the file path in the script (i.e. '/media/webmaster/Data/Gis/Math/ELCs 2012/ELCs2012.shp') with a shapefile on your local machine</li>
<li>Execute the script</li>
<li>The last line, iface.legendInterface().layers()[0].dataProvider().name(), should print 'ogr' in the python console, but it'll crash and throw an error</li>
</ol>
<p>If you check iface.legendInterface().layers()[0], you'll notice it is considered to be a QgsMapLayer, which is the wrong cast, and explains why dataProvider() isn't accessible.</p> QGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=729872016-10-12T18:45:34ZMartin Dobiaswonder.sk@gmail.com
<ul></ul><p>There is indeed some change in the internals, not sure yet if it is related to SIP or to the switch to Python3.</p>
<p>It can be replicated even just in the console. It seems that when the temporary python wrapper of QgsMapLayerRegistry instance is deleted, things go wrong in QGIS 3. Also worth noting that for some reason the layer initially has 4 refs in QGIS 2, while only 3 refs in QGIS 3 - the missing reference may be the reason why things go wrong (QgsMapLayerRegistry has the same number of refs in both QGIS 2 and QGIS 3).</p>
<p>I made a series of dumps with SIP:</p>
<p>1. after being added to registry (registry referenced in console)</p>
<pre>
>>> r = QgsMapLayerRegistry.instance()
>>> QgsMapLayerRegistry.instance().addMapLayer(QgsVectorLayer('my_layer.shp', 'testing', 'ogr'))
>>> QgsMapLayerRegistry.instance().mapLayers()
>>> sip.dump(iface.legendInterface().layers()[0])
</pre>
<p>2. after garbage collection (registry still referenced in console)</p>
<pre>
>>> gc.collect()
>>> sip.dump(iface.legendInterface().layers()[0])
</pre>
<p>3. after getting rid of registry reference in console</p>
<pre>
>>> del r
>>> gc.collect()
>>> sip.dump(iface.legendInterface().layers()[0])
</pre>
<p>Output from QGIS 3.0</p>
<pre>
<qgis._core.QgsVectorLayer object at 0x7f59f53420d8>
Reference count: 3
Address of wrapped object: 0x5045080
Created by: Python
To be destroyed by: C/C++
Parent wrapper: <qgis._core.QgsMapLayerRegistry object at 0x7f59f5342168>
Next sibling wrapper: NULL
Previous sibling wrapper: NULL
First child wrapper: NULL
<qgis._core.QgsVectorLayer object at 0x7f59f53420d8>
Reference count: 2
Address of wrapped object: 0x5045080
Created by: Python
To be destroyed by: C/C++
Parent wrapper: <qgis._core.QgsMapLayerRegistry object at 0x7f59f5342168>
Next sibling wrapper: NULL
Previous sibling wrapper: NULL
First child wrapper: NULL
<qgis._core.QgsMapLayer object at 0x7f59f53420d8>
Reference count: 1
Address of wrapped object: 0x5045080
Created by: C/C++
To be destroyed by: C/C++
Parent wrapper: NULL
Next sibling wrapper: NULL
Previous sibling wrapper: NULL
First child wrapper: NULL
</pre>
<p>Output from QGIS 2.18</p>
<pre>
<qgis._core.QgsVectorLayer object at 0x7efcb04f0b98>
Reference count: 4
Address of wrapped object: 0x639d000
Created by: Python
To be destroyed by: C/C++
Parent wrapper: <qgis._core.QgsMapLayerRegistry object at 0x7efcb04f0b00>
Next sibling wrapper: NULL
Previous sibling wrapper: NULL
First child wrapper: NULL
<qgis._core.QgsVectorLayer object at 0x7efcb04f0b98>
Reference count: 3
Address of wrapped object: 0x639d000
Created by: Python
To be destroyed by: C/C++
Parent wrapper: <qgis._core.QgsMapLayerRegistry object at 0x7efcb04f0b00>
Next sibling wrapper: NULL
Previous sibling wrapper: NULL
First child wrapper: NULL
<qgis._core.QgsVectorLayer object at 0x7efcb04f0b98>
Reference count: 2
Address of wrapped object: 0x639d000
Created by: Python
To be destroyed by: C/C++
Parent wrapper: NULL
Next sibling wrapper: NULL
Previous sibling wrapper: NULL
First child wrapper: NULL
</pre> QGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=729882016-10-12T20:48:44ZMathieu Pellerin - nIRVnirvn.asia@gmail.com
<ul></ul><p>Sharing some more experiment.</p>
<p>The following code <strong>does not</strong> work:<br /><pre>
def load(fileName):
qgslayer = QgsVectorLayer(fileName, 'testing', 'ogr')
QgsMapLayerRegistry.instance().addMapLayers([qgslayer])
load('/media/webmaster/Data/Gis/Math/ELCs 2012/ELCs2012.shp')
iface.legendInterface().layers()[0].dataProvider().name()
</pre></p>
<p>The following code <strong>does</strong> work:<br /><pre>
qgslayer = None
def load(fileName):
global qgslayer
qgslayer = QgsVectorLayer(fileName, 'testing', 'ogr')
QgsMapLayerRegistry.instance().addMapLayers([qgslayer])
load('/media/webmaster/Data/Gis/Math/ELCs 2012/ELCs2012.shp')
iface.legendInterface().layers()[0].dataProvider().name()
</pre></p> QGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=729892016-10-12T21:23:15ZVictor Olayavolayaf@gmail.com
<ul></ul><p>IF it's not a Processing bug, shouldn't we reopen it with another name to give it more visibility?</p>
<p>I think some devs might overlook it thinking it's only related to Processing...</p> QGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=730352016-10-13T22:45:17ZMathieu Pellerin - nIRVnirvn.asia@gmail.com
<ul><li><strong>Subject</strong> changed from <i>processing: file-based layers added as a result of an algorithm are missing a dataProvider() object, breaking subsequent algorithms</i> to <i>python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deleted</i></li><li><strong>Assignee</strong> deleted (<del><i>Victor Olaya</i></del>)</li></ul><p>Victor, I've renamed the title of this issue and have removed the assigned to value to indicate the need to address this severe regression.</p> QGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=730862016-10-16T21:42:21ZMatthias Kuhn
<ul></ul><p>Can you check the PR 3611?</p>
<p><a class="external" href="https://github.com/qgis/QGIS/pull/3611">https://github.com/qgis/QGIS/pull/3611</a></p>
<p>I don't really have an explanation why that should fix anything but cleaned some sip conversion code and it seems at least no longer to return any "QgsMapLayer" typed objects.</p> QGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=730882016-10-16T23:45:00ZMathieu Pellerin - nIRVnirvn.asia@gmail.com
<ul></ul><p>Matthias, it <strong>did</strong> fix this issue, I've got a working processing tool (db manager, etc.) again! :)</p> QGIS Application - Bug report #15683: python/sip: qgsvectorlayer instance improperly turns into qgsmaplayer when reference variable deletedhttps://issues.qgis.org/issues/15683?journal_id=730892016-10-16T23:46:57ZAnonymous
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Fixed in changeset <a class="changeset" href="https://issues.qgis.org/projects/qgis/repository/revisions/03f08a6c79fc4016d7ed0b89262d916cb3b70bab" title="Fix QgsMapLayer ConvertToSubClassCode (#3611) Fix #15683">03f08a6c79fc4016d7ed0b89262d916cb3b70bab</a>.</p>