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

Andreas Beckermann b_mann at gmx.de
Wed Sep 24 03:58:53 CEST 2003


On Wednesday 24 September 2003 02:22, cantabile wrote:
> Sorry, Andreas,
> I was wrong. I searched again the doc and found I had mixed infos about
> QDataStream::setRawBytes ( which I was struggling with in another part
> of my code) and the warning about deleting twice items in a QPtrList set
> to autoDelete...
> Thus, I was convinced I had read that setAutoDelete(true) was
> discouraged (the warning in setrawBytes about 'inexperienced
> programmers' - like me), and I thought you meant 'false' instead of
> 'true'.
>
> Anyway, this remark wasn't meant to be offensive and I hope you weren't
> offended.

I just was a bit surprised to read it should be avoided. I and many others use 
it regulary.

> BTW, 'true' in my code makes the program crash, even with the new play()
> method you kindly rewrote for me. Looks like I'm stepping into this
> double deletion warning, although I don't understand why.

When you use setAutoDelete(true), you need to take care that you don't delete 
items on your own. I do not remember what solution we ended up with, mixing 
that solution with setAutoDelete(true) is most probably a bad idea.

If you use setAutoDelete(true) you must NOT use things like
Foo* bar = list.at(x);
delete bar;
list.clear();
as "bar" is still in the list here. When it is cleared, you have a double 
deleteion due to the autodeletion facility. You must use something like
list.remove(x);
list.clear();
instead. The clear() statement won't have any effect in the item at x, as it 
has been removed (and deleted if auto deletion is activated) already.

Of if 2 lists contain the same pointers, you need to take care that 1 list 
uses auto deletion (if at all) and that the item is removed from the other 
list before it gets deleted. But you'll also need to do something similar if 
you don't use auto deletion at all.

> Cheers.

CU
Andi


More information about the kde-games-devel mailing list