[KPhotoAlbum] Deallocation in KPA

Tuomas Suutari tuomas.suutari at gmail.com
Sun Jan 22 11:47:10 GMT 2012


Hi, Steffen!

Here's some quick answers to your deallocation questions. Hopefully
these will help too.

On 18 January 2012 10:50, Steffen Jost <steffenjost at web.de> wrote:
> How is deallocation handled in KPA?

In many cases the deallocation has been left as parent QObject's
responsibility and there is also many stack objects or value-type
members which are deallocated automatically.

Let's explain this in more detail. QObjects form trees. When the root
object of a QObject-tree is deleted (by explicit delete statement or
by other means), the root object will delete all its direct children
which will delete their children and so on, to the leafs. So a whole
tree of objects can be deleted from single point in QObject case.
Note: If you have a pointer to a QObject that is a child of another
QObject, it's possible that the pointer will become dangling at some
point, if the parent is deleted while you hold the pointer. Therefore
it's not usually a good idea to delete parented QObject pointers
directly. (IIRC the child QObject will detach itself from the parent
in its destructor though.)

Stack objects are constructed in a scope and C++ will automatically
destruct them when the scope ends. Stack objects are those that are
NOT created with a new statement. You should learn the difference
between heap objects and stack objects, if you want to code in C++.
Value-type members are constructed in the class constructor and
destructed in the class destructor automatically by the language. You
know those by the absence of * in the member declaration in class
definition (in the h file).

> The current HTMLDialog already has a QList, QMap and an IdList(?) for which
> I cannot find any qDeleteAll. Is this a memory leak?

I checked those:
 * The QList has pointers to ImageSizeCheckBox objects which are
parented. => No need for explicit delete.
 * The _themes QMap is from int to QString. Both types are value-types
and therefore no need for delete.
 * The _whatToIncludeMap QMap is from QString to QCheckBox*. QString
is value-type, so no need for delete and the QCheckBox pointers that
are inserted to the map point to parented QObjects and therefore no
need to delete them either.
 * IdList is a value-type member, so no need to delete it. (Deletion
of its contents is responsibility of the IdList.)

But of course, like Miika said, it's possible that there is many
deallocation bugs too, so be careful when using the code as an
example.

-- 
Tuomas



More information about the Kphotoalbum mailing list