qt-interest@trolltech.com
[Top] [All Lists]

Re: QValueList (List of lists problem).

Subject: Re: QValueList (List of lists problem).
From: José Román Bilbao
Date: Thu, 13 Jan 2005 10:28:53 +0100
Hi, thanks for your response... 

It seems that the problem comes from a point before this code I thought
it was working properly as nothing failed. Before all the code pasted
yesterday I do this:

Basically I have lists of lists, storing for example (element by
element):

params_combinations is the type QValueList< QValueList < QString> > and
stores something like:

-a 1 -b 2 -c 4
-a 2 -b 2.1 -c 5
-a 1 -b 3 -c 2
-a 2 -b 23 -c1

Imagine I need to order them depending on the value corresponding with
-a, in this case 1 or 2. That is what I am trying to do with this code,
just pair sets of parameters. It seems there is something I have not
understood about iterators because I don't really know what I am doing
bad.

void grid_principal::pair_for_resolution()
{
    QValueList< QString> aux, aux2;
    QValueList< QValueList< QString> > params_aux;
            
    while( params_combinations.count() > 0 )
    {
        aux = params_combinations[ 0 ];
        params_combinations.remove( params_combinations.begin() );
        params_aux.append( aux );
        aux2 = find_pair( aux );
        params_combinations.remove( aux2 );
        params_aux.append( aux2 );
    }
    
    params_combinations.clear();
    params_combinations = params_aux;
    
    params_aux.clear();
}

QValueList< QString>  grid_principal::find_pair( QValueList<QString>
object )
{
    bool is_the_pair;
    
    QValueList< QValueList< QString> >::iterator list_iterator;
    QValueList< QString>::iterator element_iterator,object_iterator;
    
    for( list_iterator = params_combinations.begin() ; list_iterator !=
params_combinations.end() ; list_iterator ++ )
    {
        is_the_pair = true;
        
        object_iterator = object.begin();
        
        for( element_iterator = (*list_iterator).begin() ; element_iterator !=
(*list_iterator).end() ;  )
        {
            if( ( *element_iterator ).toInt() != NO_VALUE )
            {
                element_iterator++;
                object_iterator++;
                
                if( ( *element_iterator ) != QString("-o"))
                {
                    element_iterator++;
                    object_iterator++;
                                                        
                    if( ( *element_iterator ) != ( *object_iterator ) )
                    {
                        is_the_pair = false;
                        break;
                    }
                }    
            }
            else
            {
                element_iterator++;
                object_iterator++;              
            }
        }       
        
        if( is_the_pair == true )
        {
            return ( *list_iterator );
        }
    }
}

On Thu, 2005-01-13 at 09:23 +0000, Dan Milburn wrote:
> Josà RomÃn Bilbao wrote:
> 
> > Here you are the backtrace...
> > 
> > [New Thread 1102678960 (LWP 16737)]
> > 
> > Program received signal SIGSEGV, Segmentation fault.
> > [Switching to Thread 1102678960 (LWP 16737)]
> > 0x4087aeeb in QString::QString () from /usr/lib/libqt-mt.so.3
> > (gdb) backtrace
> > #0  0x4087aeeb in QString::QString () from /usr/lib/libqt-mt.so.3
> > #1  0x08075875 in QValueListPrivate<QString>::insert (this=Variable
> > "this" is not available.
> > ) at qvaluelist.h:62
> > #2  0x08075941 in QValueListPrivate (this=Variable "this" is not
> > available.
> > ) at qvaluelist.h:105
> > #3  0x0807599f in QValueList<QString>::detachInternal (this=Variable
> > "this" is not available.
> > ) at qvaluelist.h:629
> 
> It's pretty obvious that you're trying to access an invalid iterator - 
> 'this=Variable "this" is not available' being a big clue..
> 
> Looking at the code the problem is that you're incrementing 
> list_iterator before the second nested for loop and not checking whether 
> it is still valid.  So the solution is to put:
> 
>      ++list_iterator;
>      if( list_iterator == params_combinations.end() )
>          break;
> 
> or something similar.
> 
> Also, why are you decrementing then incrementing list_iterator inside 
> that second loop?
> 
> 
> Dan
> 
> --
> List archive and information: http://lists.trolltech.com/qt-interest/

--
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>