<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;}
span.EmailStyle22
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">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.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> QGIS-Developer <qgis-developer-bounces@lists.osgeo.org>
<b>On Behalf Of </b>Benjamin Jakimow via QGIS-Developer<br>
<b>Sent:</b> Thursday, May 4, 2023 3:21 AM<br>
<b>To:</b> qgis-developer@lists.osgeo.org<br>
<b>Subject:</b> Re: [QGIS-Developer] RuntimeError: wrapped C/C++ object of type QgsVectorLayer has been deleted<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p>Hi Luke,<o:p></o:p></p>
<p>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.
<br>
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.<o:p></o:p></p>
<p>In that case your self.aoi_layer python reference still exists, but not the underlying C++ object.<o:p></o:p></p>
<p>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).<br>
Or save the aoi_layer_id and test in your eventFilter, if the aoi_layer instance still exists:<o:p></o:p></p>
<p>aoi_layer = self.project().mapLayers().get(self.aoi_layer_id)<o:p></o:p></p>
<p>if isinstance(aoi_layer, QgsMapLayer):<br>
   aoi_layer_data_provider = aoi_layer.dataProvider()<o:p></o:p></p>
<p>  ...<o:p></o:p></p>
<p><o:p> </o:p></p>
<p>Greetings,<o:p></o:p></p>
<p>Benjamin<o:p></o:p></p>
<div>
<p class="MsoNormal">Am 04.05.2023 um 04:46 schrieb Catania, Luke A ERDC-RDE-GRL-VA CIV via QGIS-Developer:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal">I am trying to understand the general meaning of the message below. 
<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">RuntimeError: wrapped C/C++ object of type QgsVectorLayer has been deleted<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">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.<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">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:<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">    def eventFilter(self, source, event):<o:p></o:p></p>
<p class="MsoNormal">          if event.type() == QEvent.MouseButtonPress and event.button() == Qt.RightButton:<o:p></o:p></p>
<p class="MsoNormal">                        if self.valid_size(self.aoi_area):<o:p></o:p></p>
<p class="MsoNormal">                            aoi_layer_data_provider = self.aoi_layer.dataProvider()<o:p></o:p></p>
<p class="MsoNormal">                            self.aoi_feature.setGeometry(self.aoi_geometry)<o:p></o:p></p>
<p class="MsoNormal">                            aoi_layer_data_provider.addFeatures([self.aoi_feature])<o:p></o:p></p>
<p class="MsoNormal">                            self.aoi_layer.commitChanges()<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">                            self.aoi_layer.setExtent(self.aoi_feature.geometry().boundingBox())<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">                            self.add_area_perimeter()<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">                            self._refresh_and_zoom_to_layer(True)<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">                            self.AOI_SIZE_VALIDATOR_LABEL.close()<o:p></o:p></p>
<p class="MsoNormal">                            self.track_cursor = False<o:p></o:p></p>
<p class="MsoNormal">                            iface.mapCanvas().viewport().removeEventFilter(self)<o:p></o:p></p>
<p class="MsoNormal">                              <o:p></o:p></p>
<p class="MsoNormal">    Return False<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">I initially had the return to be “return super().eventFilter(source, event)”<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">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.<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">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.<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">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.<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">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.
<o:p></o:p></p>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<pre>_______________________________________________<o:p></o:p></pre>
<pre>QGIS-Developer mailing list<o:p></o:p></pre>
<pre><a href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a><o:p></o:p></pre>
<pre>List info: <a href="Blockedhttps://lists.osgeo.org/mailman/listinfo/qgis-developerBlocked">Blockedhttps://lists.osgeo.org/mailman/listinfo/qgis-developerBlocked</a><o:p></o:p></pre>
<pre>Unsubscribe: <a href="Blockedhttps://lists.osgeo.org/mailman/listinfo/qgis-developerBlocked">Blockedhttps://lists.osgeo.org/mailman/listinfo/qgis-developerBlocked</a><o:p></o:p></pre>
</blockquote>
<pre>-- <o:p></o:p></pre>
<pre>Dr. Benjamin Jakimow<o:p></o:p></pre>
<pre>Earth Observation Lab | Geography Department | Humboldt-Universität zu Berlin<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>e-mail: <a href="mailto:benjamin.jakimow@geo.hu-berlin.de">benjamin.jakimow@geo.hu-berlin.de</a><o:p></o:p></pre>
<pre>web:    <a href="Blockedhttps://hu-berlin.de/eo-labBlocked">Blockedhttps://hu-berlin.de/eo-labBlocked</a><o:p></o:p></pre>
<pre>phone:  +49 (0) 30 2093 6846<o:p></o:p></pre>
<pre>mobile: +49 (0) 157 5656 8477<o:p></o:p></pre>
<pre>fax:    +49 (0) 30 2093 6848<o:p></o:p></pre>
<pre>mail:   Unter den Linden 6 | 10099 Berlin | Germany<o:p></o:p></pre>
<pre>room: 2'211<o:p></o:p></pre>
<pre>linkedin: <a href="Blockedhttps://www.linkedin.com/in/benjamin-jakimowBlocked">Blockedhttps://www.linkedin.com/in/benjamin-jakimowBlocked</a><o:p></o:p></pre>
<pre>matrix: @jakimowb:hu-berlin.de<o:p></o:p></pre>
</div>
</body>
</html>