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

Kevin Krammer kevin.krammer at gmx.at
Sun Sep 21 12:59:54 CEST 2003


On Saturday 20 September 2003 23:32, Andreas Beckermann wrote:
> On Saturday 20 September 2003 18:47, cantabile wrote:
> > Hi,
> > I encounter a simple problem with QPtrList, but can't see why:
> > when my game is ending, I call a clear() function, which erase the
> > values in a QPtrList<QCanvasItems>.
> >
> > When the list has not been used (e.g I call a new game at the start,
> > without erasing any value in the list), no problem, but when it's
> > been used, I get a crash pointing to my clear() methtod.
> >
> > Here's the method :
> >
> > void KMatchesView::clear()
> > {
> > 	if (!matchesList.isEmpty())
> > 	for (uint i=0; i< matchesList.count() ; i++) {
> >                 QCanvasItem* match = matchesList.at(i);
> >                 if (match->rtti() == matchRTTI) {
> >                         delete match;
>
> Are you sure you want to do this?
>
> Imagine a list with 2 elements. You start at i==0, the item
> matchesList.at(0) is deleted. Now your list has one entry only.
> The next loop iteration (i==1) will stop the loop, as
> i==matchesList.count() ==1. The second item is never deleted.

He does not remove the item from the list, so the list keeps its length, 
which you already poiinted out below, isn't a good idea.

Cheers,
Kevin
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kevin Krammer <kevin.krammer at gmx.at>
Developer at the Kmud Project http://www.kmud.de/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: signature
Url : http://mail.kde.org/pipermail/kde-games-devel/attachments/20030921/5d8768b4/attachment.bin


More information about the kde-games-devel mailing list