Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, January 2007
Qt 3.3 QListViewItemIterator::operator ++() causes stack-overflow


Message 1 in thread

Hi,

I've an insanely large QListView with about 120000 items (dont blame me, 
blame the users!). It renders fast and is usable (Qt rocks), but piece 
of code creates a big problem now: I hide some items (filter), and then 
unhide them:


old not working code: (stack overflow)

   QListViewItemIterator itp(parameter, QListViewItemIterator::Invisible);
   QListViewItem* ip;

   for(; ip=itp.current(); ++itp)
     ip->setVisible(true);


new working code:

   QListViewItemIterator itp(parameter);
   QListViewItem* ip;

   for(; ip=itp.current(); ++itp)
   {
     if(ip->isVisible() == false)
       ip->setVisible(true);
   }


after looking in the QListViewItemIterator 
&QListViewItemIterator::operator++() I think I can see the problem:
QListViewItemIterator &QListViewItemIterator::operator++()
{
     if ( !curr )
	return *this;

     QListViewItem *item = curr->firstChild();
     if ( !item ) {
	while ( (item = curr->nextSibling()) == 0  ) {
	    curr = curr->parent();
	    if ( curr == 0 )
		break;
	}
     }
     curr = item;
     // if the next one doesn't match the flags we try one more ahead
     if ( curr && !matchesFlags( curr ) )
	++( *this );
     return *this;
}

Since I'm filtering by invisible items (and in some case there are none, 
or only one) the ++ operator gets called recursive here:

     if ( curr && !matchesFlags( curr ) )
	++( *this );

With 10000 items the stack can handle it, even with 100k, but at some 
point its too much.

Shouldn't this be implemented different in Qt? Like using a loop instead 
of a recursive call? I don't know if I should tell them, or if they even 
care, but I think its stupid, but since it only affects Qt 3.3 they 
might not change it and tell me to use the workaround i found?

Any ideas/comments to this problem?

--
 [ signature omitted ] 

Message 2 in thread

Hi,

> Shouldn't this be implemented different in Qt? Like using a loop instead 

I'm not sure about this one.

Generally speaking there may be some constraints on the operations that 
may be applied to a container using an iterator pointing to an element 
of this container. For example removing from the container the item 
pointed by the iterator may be a bad idea, depending on the 
implementation of the container/iterator. Unfortunately these 
constraints are not always precisely documented.

In this specific case you're iterating over invisible items and 
modifying this property behind the back of the iterator.

In any case, if at least the crash can be avoided, it's probably worth 
fixing it.

> of a recursive call? I don't know if I should tell them, or if they even 
> care, but I think its stupid, but since it only affects Qt 3.3 they 

Trolltech certainly do care, do not hesitate to send bug reports here:
	http://www.trolltech.com/bugreport-form
They will be read with attention.

> might not change it and tell me to use the workaround i found?

If the fix changes the behavior and a workaround is available, then 
indeed there's a risk this won't be fixed in the Qt 3.3 branch. You 
won't know for sure until you send the bug report though...

--
 [ signature omitted ]