tags:

views:

33

answers:

1

Hi I have a problem with my piece of code. I am getting assertion failed message from qt when a use this code. But the message is being generated when repainting (somewhere in bst tree) the scene so i can not find out what is wrong in my code. Maybe i am missing something? Please take a look. I am using Qt 4.6

After this function program crashes

QGisWaypoint *QGisMap::removeOptimized( QGisWaypoint *from )
{
    QList<QGraphicsItem*> children = from->childItems();
    QList<QGraphicsItem*>::Iterator it = children.begin(), end = children.end();
    QGisWaypoint *child;

    // remove all child item till !Optimized
    while( it!=end )
    {
        child = qgraphicsitem_cast<QGisWaypoint*>( *it );
        Q_ASSERT( child );
        if( child->pointType() == QGisWaypoint::Optimized )
        {
            _path.erase( child );
            children = from->childItems();
            it = children.begin();
            end = children.end();
            continue;
        }
        else
            break;

        ++it;
     }
    return child;
}

The erease function

void QGisPath::erase( QGisWaypoint *item )
{
    Q_ASSERT( item );
    QList<QGraphicsItem *>children = item->childItems();
    if( children.size() )
    {
            QGraphicsItem *parent = item->parentItem();
            QGraphicsItem *child; // aktualne dziecko
            for( int j=0, c_end=children.size(); j<c_end; ++j )  // check all children
            {
                child = children[j];

                if( parent )    
                    child->setPos( parent->mapFromItem( item, child->pos() ) );
                else            
                    child->setPos( item->mapToScene( child->pos() ) );

                child->setParentItem( parent ); // new parent or child becomes a top level item if no parent
            }
    }

    if( QGraphicsScene *s = item->scene() )
        s->removeItem( item ); // item have no children so it can be removed separately
    else
        qDebug() << "Something wrong - no scene)";

    int wp_pos = indexOf( item );
    Q_ASSERT( wp_pos != -1 );
    waypoints.removeAt( wp_pos ); // waypoints is type of QList<QGisWaypoint*>
    delete item;
    touch();
}

And this code inserts the items

QLineF line;
QLineF nex_line;
QGisWaypoint *last_wp = item;
int last_wp_pos;
int s,t;
int last_source_id = start_id;

while( q.next() )
{
    if( q.value(0).toInt() == last_source_id )
    {
        s=0;
        t=1;
    }
    else
    {
        s = 1;
        t = 0;
    }
    last_source_id = q.value(t).toInt();
    last_wp_pos = _path.indexOf(last_wp) + 1;
    last_wp = make_waypoint( NULL ); // new + adding to scene + setting position
    last_wp->setPointType( QGisWaypoint::Optimized );

    line = WktToLine( q.value(2).toString() );
    last_wp->setPos( s?line.p2():line.p1() );

    _path.insert( last_wp_pos, last_wp );
}

The insert function

void QGisPath::insert( int pos, QGisWaypoint *wp )
{
    QGraphicsItem *parent = NULL;
    QList<QGraphicsItem *> children;
    QGraphicsItem *child = NULL;

    if( waypoints.size() ) // if path is empty simpli simply insert the item becouse it is a top level item
    {
        if( pos <= 0 ) // prepend?
            children.append( waypoints.first() );
        else if( pos >= waypoints.size() ) // append?
            parent = waypoints.last();
        else // insert
        {
            parent = waypoints[pos]->parentItem();
            children = parent->childItems();
            Q_ASSERT( children.size() ); // there mus be at least one child
        }

        QPointF ppos;
        if( parent )
        {
            ppos = parent->mapFromItem( wp, QPointF(0.0,0.0) ); // remap position to parent
            wp->setParentItem( parent );
            wp->setPos( ppos );
        }

        for( int i=0, end=children.size(); i<end; ++i ) // take children from parent and give them to the 'wp'
        {
            child = children[i];

            ppos = wp->mapFromItem( parent, child->pos() );
            child->setParentItem( wp );
            child->setPos( ppos );
        }
    }
    waypoints.insert( pos, wp );
    touch();
}

The program crashes in file qgraphicsscene_bsp.cpp on function

void visit(QList<QGraphicsItem *> *items)
{
    for (int i = 0; i < items->size(); ++i) {
    QGraphicsItem *item = items->at(i);
    if (onlyTopLevelItems && item->d_ptr->parent) // <-- heare is the SIGSEGV d_ptr is NULL
        item = item->topLevelItem();
    if (!item->d_func()->itemDiscovered && item->d_ptr->visible) {
    item->d_func()->itemDiscovered = 1;
        foundItems->prepend(item);
    }
}

Please give me some tip becouse a have no idea what is wrong.

A: 

I didn't solve the problem but when I build program in release version it works. I supose that this is caused by all asserts being removed from source in release version. I know that this is bad but for now it work and I have no idea how to repair it.

BeginEnd