pub unsafe fn q_add_post_routine(arg1: Option<extern "C" fn()>)
Expand description

Adds a global routine that will be called from the QCoreApplication destructor. This function is normally used to add cleanup routines for program-wide functionality.

Calls C++ function: void qAddPostRoutine(void (*FN_PTR)() arg1).

C++ documentation:

Adds a global routine that will be called from the QCoreApplication destructor. This function is normally used to add cleanup routines for program-wide functionality.

The cleanup routines are called in the reverse order of their addition.

The function specified by ptr should take no arguments and should return nothing. For example:

static int *global_ptr = 0;

static void cleanup_ptr() { delete [] global_ptr; global_ptr = 0; }

void init_ptr() { global_ptr = new int[100]; // allocate data qAddPostRoutine(cleanup_ptr); // delete later }

Note that for an application- or module-wide cleanup, qaddPostRoutine() is often not suitable. For example, if the program is split into dynamically loaded modules, the relevant module may be unloaded long before the QCoreApplication destructor is called. In such cases, if using qaddPostRoutine() is still desirable, qRemovePostRoutine() can be used to prevent a routine from being called by the QCoreApplication destructor. For example, if that routine was called before the module was unloaded.

For modules and libraries, using a reference-counted initialization manager or Qt's parent-child deletion mechanism may be better. Here is an example of a private class that uses the parent-child mechanism to call a cleanup function at the right time:

class MyPrivateInitStuff : public QObject { public: static MyPrivateInitStuff initStuff(QObject parent) { if (!p) p = new MyPrivateInitStuff(parent); return p; }

~MyPrivateInitStuff() { // cleanup goes here }

private: MyPrivateInitStuff(QObject *parent) : QObject(parent) { // initialization goes here }

MyPrivateInitStuff *p; };

By selecting the right parent object, this can often be made to clean up the module's data at the right moment.

See also qRemovePostRoutine().