[QGIS-Developer] On the danger of static C++ objects

Even Rouault even.rouault at spatialys.com
Wed Nov 15 14:58:09 PST 2017


Hi,

There was a frequent crash in PyQgsServerWMS on Travis that I've hopefully fixed per 
https://github.com/qgis/QGIS/commit/581d0d30ca89a213b1ba1a56a3fe2af5c896eacd

This is the opportunity to raise attention on the danger of using this pattern:

static SomeClass singletonInstance;

where ~SomeClass() does some non trivial work in its destructor, and in particular calling 
other libraries (such as in the above mentionned case). Depending on the order into which 
libraries are unloaded and objects destroyed,  ~SomeClass() might call a function of a 
library which has already been unloaded, leading to crash.

So I'd suggest rather using a pointer approach (which admitedly makes constructing the 
singleton in a thread-safe way a bit harder), and explicitly deleting the singleton at 
appropriate and explicit time.

Unless someone is aware of a better approach.

By the way, is there somewhere in the tree a developer guide where we can collect such 
knowledge ?

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20171115/58f9aa6b/attachment.html>


More information about the QGIS-Developer mailing list