mirror of
https://github.com/subsurface/subsurface.git
synced 2024-12-05 00:21:29 +00:00
8577b00cb7
We have a prevailing problem with global QObjects defined as static global variables. These get destructed after main() exits, which means that the QApplication object does not exist anymore. This more often than not leads to crashes. In a quick search I didn't find a mechanism to register objects for deletion with QApplication. Therefore, let's do our own list of global objects that get destructed before destroying the QApplication. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
50 lines
1.2 KiB
C++
50 lines
1.2 KiB
C++
// SPDX-License-Identifier: GPL-2.0
|
|
// Collection of objects that will be deleted on application exit.
|
|
// This feature is needed because many Qt-objects crash if freed
|
|
// after the application has exited.
|
|
#ifndef GLOBALS_H
|
|
#define GLOBALS_H
|
|
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
template <typename T, class... Args>
|
|
T *make_global(Args &&...args); // construct a global object of type T.
|
|
|
|
template <typename T>
|
|
T *register_global(T *); // register an already constructed object. returns input.
|
|
|
|
void free_globals(); // call on application exit. frees all global objects.
|
|
|
|
// Implementation
|
|
|
|
// A class with a virtual destructor that will be used to destruct the objects.
|
|
struct GlobalObjectBase
|
|
{
|
|
virtual ~GlobalObjectBase() { }
|
|
};
|
|
|
|
template <typename T>
|
|
struct GlobalObject : T, GlobalObjectBase
|
|
{
|
|
using T::T; // Inherit constructor from actual object.
|
|
};
|
|
|
|
void register_global_internal(GlobalObjectBase *);
|
|
|
|
template <typename T, class... Args>
|
|
T *make_global(Args &&...args)
|
|
{
|
|
GlobalObject<T> *res = new GlobalObject<T>(std::forward<Args>(args)...);
|
|
register_global_internal(res);
|
|
return res;
|
|
}
|
|
|
|
template <typename T>
|
|
T *register_global(T *o)
|
|
{
|
|
make_global<std::unique_ptr<T>>(o);
|
|
return o;
|
|
}
|
|
|
|
#endif
|