I developed a plugin in QGIS. One of the processes consists in remove some layers from the QGisProject, use them with other third party code in C# (maybe layers are modified by C# code) and finally open again these layers in QGIS. With QGIS 2.18 it works perfectly, but with version 3, when I try to do that, the C# code can't write on removed layers because DBF files are in use by another application (QGIS, I suppose).
I use this code to open layers:
try: #QGis 3.x QgsProject.instance().addMapLayer(vlayer, group is None) except: #QGis 2.x QgsMapLayerRegistry.instance().addMapLayer(vlayer, group is None)
Then, to remove layers I use this one:
try: #QGis 3.x QgsProject.instance().removeMapLayer(layer.id()) except: #QGis 2.x QgsMapLayerRegistry.instance().removeMapLayers([layer.id()])
I think the problem is not in these lines, because some times the process works fine. Other times it works with some layers but not with another layers... Depending of the time that I wait to run the process after open the layers.
If I manually delete the layers from the QGIS user interface and run my process, it works fine.
I have the option of saving the project in a temporal qgis project file. Clear the QGISProject and finally (after C# code) open this temporal file... but it is a dirty solution when I only need to remove one layer...
I don't know what is happening. Some idea? It's possible to free memory of the QGIS project of these removed layers?