[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Problem with QT
From:       André_Wöbbeking <Woebbeking () onlinehome ! de>
Date:       2005-10-22 9:12:35
Message-ID: 200510221112.35280.Woebbeking () onlinehome ! de
[Download RAW message or body]

On Saturday 22 October 2005 11:00, Bernd Brandstetter wrote:
> On Saturday 22 October 2005 10:40, André Wöbbeking wrote:
> >
> > ???
> >
> > If an object (in the case the the list) was destructed you MUST NOT
> > use it anymore.
>
> No, I meant something different:
> Say you have derived a _pointer-based_ list from QList
>
> class MyList : public QList<SomeClass*>
> {
>     ...
> }
>
> and since you always want all list items to be deleted along with the
> list, you put qDeleteAll(this) in the destructor of the class to
> simulate the behavior of Qt3's setAutoDelete()
>
> MyList::~MyList()
> {
>     qDeleteAll(this);
> }
>
> Now, if you happen to iterate over such a list using foreach() like
> this
>
> MyList list;
> // fill list with items
>
> foreach(SomeClass* item, list)
> {
>     // do something with the item
> }
>
> then at this point after the foreach loop has finished, the contents
> of the original list, i.e. the objects pointed to by the list items,
> have been deleted.
>
> That's because foreach() creates a temporary copy of the list and
> iterates over that one. After the iteration, the temporary list is
> deleted, thereby calling the destructor defined above and thus,
> deleting all items!

I see. This sounds a bit strange and I don't know if it's really good to 
create a temporary copy.

> This occured to me when I ported an application from Qt3 to Qt4 and
> it took me quite some time to find out why it kept crashing. Now my
> rule of thumb is: _Never_ call qDeleteAll(this) in the destructor of
> _pointer-based_ collections!

On the other side: if the list really takes over ownership you should 
implement the copy constructor and assignment operator or forbid both 
methods.


Cheers,
André
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic