[QGIS-Developer] RuntimeError: wrapped C/C++ object of type QgsVectorLayer has been deleted

Catania, Luke A ERDC-RDE-GRL-VA CIV Luke.A.Catania at erdc.dren.mil
Thu May 4 17:14:39 PDT 2023


So I discarded my changes and started over and I also changed the event to end the drawing from the left mouse button to the middle and managed to avoid the deleted object error.  I tested a multitude of shape drawings and clicking the middle button to end my edit session when the size is not large enough and it ignores ending the edit session and completing the polygon until I middle click and the size is valid.

If I change the event to the right mouse button I get the error again, so this is making me believe that I am not fully overriding the event for the right mouse button and it works for the middle button because the map canvas does not use the middle mouse button for any event when a feature is being drawn.

While this great that it works with middle mouse button, I want the edit session to stop when the user clicks right not middle as that is the way QGIS works for completing an edit session.

This is vert frustrating. 😒

From: QGIS-Developer <qgis-developer-bounces at lists.osgeo.org> On Behalf Of Catania, Luke A ERDC-RDE-GRL-VA CIV via QGIS-Developer
Sent: Thursday, May 4, 2023 2:05 PM
To: Benjamin Jakimow <benjamin.jakimow at geo.hu-berlin.de>; qgis-developer at lists.osgeo.org
Subject: Re: [QGIS-Developer] RuntimeError: wrapped C/C++ object of type QgsVectorLayer has been deleted

Thanks.  I’m just trying to figure out how this happens because I don’t remove the layer from the map.  It’s actually initially added as an empty layer and then the geometry is added and then the changes are committed, so I never remove it.  I’ll review my code again to see if I do anything odd.

From: QGIS-Developer <qgis-developer-bounces at lists.osgeo.org<mailto:qgis-developer-bounces at lists.osgeo.org>> On Behalf Of Benjamin Jakimow via QGIS-Developer
Sent: Thursday, May 4, 2023 3:21 AM
To: qgis-developer at lists.osgeo.org<mailto:qgis-developer at lists.osgeo.org>
Subject: Re: [QGIS-Developer] RuntimeError: wrapped C/C++ object of type QgsVectorLayer has been deleted


Hi Luke,

to my experience the "RuntimeError: wrapped C/C++ object of type QgsVectorLayer has been deleted" is raised when the C++ QgsVectorLayer has been deleted by a parent class.
This is often a QgsMapLayerStore, for example the QgsProject.instance().mapLayerStore(), from which a layer gets removed when it is removed from the layer tree.

In that case your self.aoi_layer python reference still exists, but not the underlying C++ object.

You could ensure that the aoi_layer is not removed from the layer store it is stored in, for example using QgsLayerStore::takeMapLayer(layer) instead QgsLayerStore::removeMapLayer(layer).
Or save the aoi_layer_id and test in your eventFilter, if the aoi_layer instance still exists:

aoi_layer = self.project().mapLayers().get(self.aoi_layer_id)

if isinstance(aoi_layer, QgsMapLayer):
   aoi_layer_data_provider = aoi_layer.dataProvider()

  ...



Greetings,

Benjamin
Am 04.05.2023 um 04:46 schrieb Catania, Luke A ERDC-RDE-GRL-VA CIV via QGIS-Developer:
I am trying to understand the general meaning of the message below.

RuntimeError: wrapped C/C++ object of type QgsVectorLayer has been deleted

I step through my code in the debugger and at some point my variable, aoi_layer, is longer assigned to a specific vector layer, though the value comes back as QgsVectorLayer since I have a watch on it.  I though maybe somehow it got set to “None”, but it’s not None.

I’d like to post code, but I’d have way to much code to post.  I am basically trying to override the map canvas events with an eventFIlter while I go through a drawing process.  I want a particular action to occur when I right click on the map canvas.  I am trying to override the map canvas from completing a drawing of a shape if the shape is too small, but it does not seem to override it. The shape is completed when I right click, so it seems like I have not overridden the event even though I capture the event using the code below:

    def eventFilter(self, source, event):
          if event.type() == QEvent.MouseButtonPress and event.button() == Qt.RightButton:
                        if self.valid_size(self.aoi_area):
                            aoi_layer_data_provider = self.aoi_layer.dataProvider()
                            self.aoi_feature.setGeometry(self.aoi_geometry)
                            aoi_layer_data_provider.addFeatures([self.aoi_feature])
                            self.aoi_layer.commitChanges()

                            self.aoi_layer.setExtent(self.aoi_feature.geometry().boundingBox())

                            self.add_area_perimeter()

                            self._refresh_and_zoom_to_layer(True)

                            self.AOI_SIZE_VALIDATOR_LABEL.close()
                            self.track_cursor = False
                            iface.mapCanvas().viewport().removeEventFilter(self)

    Return False

I initially had the return to be “return super().eventFilter(source, event)”

So the shape is completed and it adds it to the layer that is open for editing.  My function allows the drawing to continue because my check indicates the shape is not large enough so the commit should not happen unless the size is valid.

I can continue drawing but I right click on the map canvas it completes the drawing a second time, so now I have two rectangles, both with the same start position, but one larger than the other.  It is when I create a second layer to draw that I get the runtime error.

So I have two problems.  I don’t seem to be completely overriding the build in event handler and QGIS is still completing the drawing when I right click. And somehow this may be contributing to the runtime error.

If I can understand in general how my object is deleted when I don’t have any statement that deletes the object maybe I can figure it out the second issue.


_______________________________________________

QGIS-Developer mailing list

QGIS-Developer at lists.osgeo.org<mailto:QGIS-Developer at lists.osgeo.org>

List info: Blockedhttps://lists.osgeo.org/mailman/listinfo/qgis-developerBlocked

Unsubscribe: Blockedhttps://lists.osgeo.org/mailman/listinfo/qgis-developerBlocked

--

Dr. Benjamin Jakimow

Earth Observation Lab | Geography Department | Humboldt-Universität zu Berlin



e-mail: benjamin.jakimow at geo.hu-berlin.de<mailto:benjamin.jakimow at geo.hu-berlin.de>

web:    Blockedhttps://hu-berlin.de/eo-labBlocked

phone:  +49 (0) 30 2093 6846

mobile: +49 (0) 157 5656 8477

fax:    +49 (0) 30 2093 6848

mail:   Unter den Linden 6 | 10099 Berlin | Germany

room: 2'211

linkedin: Blockedhttps://Blockedwww.linkedin.com/in/benjamin-jakimowBlockedBlocked

matrix: @jakimowb:hu-berlin.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230505/e4744340/attachment.htm>


More information about the QGIS-Developer mailing list