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

Issue #628 closed
Agustin Lobo created an issue

I get this error from time to time (qgis 3.16.3 on windows; enmapbox 3.7.20210202T131458.master). I’m not being able to find reproducibility, it is kind of erratic. It is not linked to any specific image or vector. Normally, a fresh start solves the problem. Sometimes even just removing the layer and trying again solves the problem:

2021-02-19T18:51:36     WARNING    Traceback (most recent call last):
              File "C:/Users/1/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\enmapboxplugin\enmapbox\gui\datasourcemanager.py", line 233, in addSources
              added.extend(self.addSource(s))
              File "C:/Users/1/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\enmapboxplugin\enmapbox\gui\datasourcemanager.py", line 248, in addSource
              knownStrings = self.uriList() + self.layerIds() + self.layerSources()
              File "C:/Users/1/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\enmapboxplugin\enmapbox\gui\datasourcemanager.py", line 211, in layerSources
              return [ds.mapLayer().source() for ds in self.sources() if isinstance(ds, DataSourceSpatial)]
              File "C:/Users/1/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\enmapboxplugin\enmapbox\gui\datasourcemanager.py", line 211, in 
              return [ds.mapLayer().source() for ds in self.sources() if isinstance(ds, DataSourceSpatial)]
             RuntimeError: wrapped C/C++ object of type QgsRasterLayer has been deleted

Comments (3)

  1. Benjamin Jakimow

    Hi Agustin,

    this is a well known error which we hope to remove soon. It is raised when a QgsMapLayer is deleted by the C++ QGIS API, but still refered by python code.

    Background:
    We like to make each (i) spatial datasource in the EnMAP-Box data source panel, as well as (ii) vector or raster layer in the EnMAP-box Layer Trees (as there is one for each EnMAP-box map canvas) visible in the QgsMapLayerComboBox. These QgsMapLayerComboBox are, for example, used in many algorithms to selecte vecotr or raster layers.

    The complicated issue is, that the QgsMapLayerComboBox only shows layers which are added to the main QGIS layer tree. Therefore we need to add a “hidden” layer there for each layer or data source in the EnMAP-Box as well. If such a layer is deleted from the QGIS layer tree and destroyed by the QGIS C++ API, it might happen that there is still a python reference to it. In this case (line 211 ds.mapLayer() ) the C/C++ error is raised.

  2. Andreas Janz

    This error can be caused by doing different things inside the EnMAP-Box. In this general form, the issue isn't very useful. We need to report specific issues that are reproducable. E.g. #713

  3. Log in to comment