Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QObjectCleanupHandler Class Reference

The QObjectCleanupHandler class watches the lifetime of multiple QObjects. More...

    #include <QObjectCleanupHandler>

Inherits QObject.

- List of all members, including inherited members

### Writable Properties

- 1 writable property inherited from QObject

### Public Functions

- QObjectCleanupHandler ()
- ~QObjectCleanupHandler ()
- QObject * add ( QObject * object )
- void clear ()
- bool isEmpty () const
- void remove ( QObject * object )

- 24 public functions inherited from QObject

### Public Slots

- 1 public slot inherited from QObject

### Signals

- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QObject

### Protected Functions

- 9 protected functions inherited from QObject

## Detailed Description

The QObjectCleanupHandler class watches the lifetime of multiple QObjects.

A QObjectCleanupHandler is useful whenever you need to know when a number of QObjects that are owned by someone else have been deleted. This is important, for example, when referencing memory in an application that has been allocated in a shared library.

To keep track of some QObjects, create a QObjectCleanupHandler, and add() the objects you are interested in. If you are no longer interested in tracking a particular object, use remove() to remove it from the cleanup handler. If an object being tracked by the cleanup handler gets deleted by someone else it will automatically be removed from the cleanup handler. You can delete all the objects in the cleanup handler with clear(), or by destroying the cleanup handler. isEmpty() returns true if the QObjectCleanupHandler has no objects to keep track of.

Example:

        class FactoryComponent : public FactoryInterface, public QLibraryInterface
        {
        public:
            ...

            QObject *createObject();

            bool init();
            void cleanup();
            bool canUnload() const;

        private:
            QObjectCleanupHandler objects;
        };

        // allocate a new object, and add it to the cleanup handler
        QObject *FactoryComponent::createObject()
        {
            return objects.add(new QObject());
        }

        // QLibraryInterface implementation
        bool FactoryComponent::init()
        {
            return true;
        }

        void FactoryComponent::cleanup()
        {
        }

        // it is only safe to unload the library when all QObject's have been destroyed
        bool FactoryComponent::canUnload() const
        {
            return objects.isEmpty();
        }

## Member Function Documentation

### QObjectCleanupHandler::QObjectCleanupHandler ()

Constructs an empty QObjectCleanupHandler.

### QObjectCleanupHandler::~QObjectCleanupHandler ()

Destroys the cleanup handler. All objects in this cleanup handler will be deleted.

See also clear().

### QObject * QObjectCleanupHandler::add ( QObject * object )

Adds object to this cleanup handler and returns the pointer to the object.

See also remove().

### void QObjectCleanupHandler::clear ()

Deletes all objects in this cleanup handler. The cleanup handler becomes empty.

See also isEmpty() and ~QObjectCleanupHandler().

### bool QObjectCleanupHandler::isEmpty () const

Returns true if this cleanup handler is empty or if all objects in this cleanup handler have been destroyed; otherwise return false.

See also add(), remove(), and clear().

### void QObjectCleanupHandler::remove ( QObject * object )

Removes the object from this cleanup handler. The object will not be destroyed.

See also add().