[Kde-games-devel] Re: test QPtrList existence ?

Ian Wadham ianw at netspace.net.au
Thu Sep 25 07:16:00 CEST 2003


Maybe I'm just summing up, but I believe setAutoDelete(true)
is quite safe when used as follows.  First create the list-header
when the application starts and leave it there till the app
terminates.  So in your application initialisation do:

    list = new QPtrList<QCanvasSprite>();
    list->setAutoDelete (true);

Then, have a method to add an element, e.g:
{
    QCanvasSprite * element = new QCanvasSprite (...);
    list->append (element);
    // Set x, y and z for "element" ... etc.
    element->show();
}  // The "element" pointer disappears as the method exits.

Then, to delete all list elements and the underlying objects
and be ready to play again, have a method that simply does:
{
    list->clear();
}

To avoid deletion problems, make sure that all references
to the underlying objects are via the list or via temporary
pointers copied from the list (mainly to avoid writer's
cramp).  These should all disappear when their method
exits and should never be used in a "delete".  If you need to
delete a single element and underlying object, use
"list->remove(n)", as Andi has suggested.

Oooo, I haven't done "delete list;", but everything goes
to that great bit-bucket in the sky when your application
terminates, doesn't it?  There's no global heap in KDE, is
there?  Is there? (sweat...)

All the best, Ian W.

PS. I like to use pointers for *all* objects, rather than "." and
"->" mixed --- for visibility, readability and consistency, at
the cost of a few nanoseconds.  Also, I'm less likely to forget
elsewhere which one I did and cause a crash or an obscure
error message ... :-)=== ... (smiley with long grey beard).


More information about the kde-games-devel mailing list