TransWikia.com

Why segmentation fault when QgsVectorlayer not deleted?

Geographic Information Systems Asked on July 14, 2021

Config: Ubuntu 20.04, QGIS 3.10, Python 3.8.5

My pyqgis scripts are doing the job, but some of them end with a segmentation fault when executing the closing instruction .exitQgis().

The segfault appear each time that I instantiate a QgsVectorLayer that does not get deleted before the end of the script.

Adding a del my_vectorlayer before the end solves the segfault. In the following example it is a bit trivial, but on longer processes using classes, such detail can be easily forgotten as I’m not used to delete explicitly variables.

Is it normal?
Is there another way of doing it better?

I have been looking in documentation but didn’t find a proper explanation.

# -*- coding: utf-8 -*-
import sys
sys.path.insert(0, '/usr/lib/python3/dist-packages')

from qgis.core import (
    QgsApplication,
    QgsVectorLayer
)

print('start app')
QgsApplication.setPrefixPath("/usr", True)
myapp = QgsApplication([], False)
myapp.initQgis()

test_vector_file = 'test.gpkg'

print('process layer')

my_layer = QgsVectorLayer(test_vector_file, 'test layer', 'ogr')

# if del instruction omitted => Segmentation fault (core dumped)
del my_layer

myapp.exitQgis()
print('closed')

Segmentation fault (core dumped) happens after the print(‘closed’)

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP