Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

void qAddPostRoutine ( QtCleanUpFunction  ptr  )  [related]

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

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 QApplication destructor is called.

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
            static MyPrivateInitStuff *initStuff(QObject *parent)
                if (!p)
                    p = new MyPrivateInitStuff(parent);
                return p;

                // cleanup goes here

            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.

Definition at line 121 of file qcoreapplication.cpp.

References QList< T >::prepend().

    QVFuncList *list = postRList();
    if (!list)

Generated by  Doxygen 1.6.0   Back to index