[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
Wed May 3 19:46:39 PDT 2023


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230504/c1d981fa/attachment.htm>


More information about the QGIS-Developer mailing list