creating a scene crashes program when I close dialog

  • Hi All,
    I've got a test for graphics view in a dialog constructor. To load an image. It works fine. However when I click on X to close the dialog it crashes the program. What I've found if I use a q scene ( which I need to ) this causes a crash and seg fault. Scene is defined in my dialog class. My code is

       QPixmap pix( "/home/tjh/Desktop/test/test.png" );
        QGraphicsPixmapItem* pixMapItem = new QGraphicsPixmapItem();
        pixMapItem->setPixmap( pix );
        pixMapItem->setPos( 20, 20 );
       scene = new QGraphicsScene( this );
       scene->setSceneRect( ui->graphicsView->rect() );
       ui->graphicsView->setScene( scene );
           scene->addItem( pixMapItem );

    Can anyone advise why this would happen? Thanks

  • Lifetime Qt Champion

    @tony67 said:
    ui->graphicsView is also defined in same dialog?

  • Moderators

    What's the stack trace? Do you explicitly delete the scene in the destructor of the dialog, try not doing it and see how it goes.

    Kind regards.

  • Hi ,
    I believe I'm closing in deconstructor, I've tried every combination of the following, eg with all three, with only ui and scene. So
    Should add made pixmapitem part of class in case this was issue, made no difference.

        delete pixMapItem;
        delete scene;
        delete ui;

    my stack shows
    0 _GI_rise
    1 GI_abort
    2 _libc_message
    3 _GI__fortify_fail
    4 _stack_Chk_fail
    5 MainWindow::on_actionPlay_Game_triggered
    6 MainWindow::qt_static_metacall
    7 MainWindow::qt_metacall
    8 QMetaObject::active(QObject* int, int, void**)

    Can anyone advise on this please?

  • Lifetime Qt Champion

    @tony67 said:

    scene = new QGraphicsScene( this );

    Here you give "this" as parent. If that is dialog, dialog owns the scene and will delete
    when its delted itself.

    So try to
    // delete scene;
    so you dont also delete it.

  • Moderators

    You had corrupted the memory somewhere (probably in the MainWindow::on_actionPlay_Game_triggered slot) judging from the stack. So focus on that, instead of thinking about the destructor. My guess it you have either overflowed a buffer, or you are modifying some foreign memory. Check the slot, and if in need of help post it's contents here.

    Kind regards.

  • Hi
    First thankyou for all the advise.
    it's a bit odd, too me at least as I'm just doing the following

    void MainWindow::on_actionPlay_Game_triggered()
    bunch of warning boxes here....
        Game_TestGame_Dialog mGame_TestGame_Dialog;
        mGame_TestGame_Dialog.exec( );

    when I step through it crashes on leaving the action trigger, so on entering the stack is
    0 MainWindow::on_actionPlay_Game_triggered mainwindow.cpp 701 0x41694c
    1 MainWindow::qt_static_metacall moc_mainwindow.cpp 192 0x566b41
    2 MainWindow::qt_metacall moc_mainwindow.cpp 224 0x566c38
    3 QMetaObject::activate(QObject*, int, int, void**) /usr/lib/x86_64-linux-gnu/ 0x7ffff6fb4ae2
    4 QAction::triggered(bool) /usr/lib/x86_64-linux-gnu/ 0x7ffff7883722
    5 QAction::activate(QAction::ActionEvent) /usr/lib/x86_64-linux-gnu/ 0x7ffff7885e28
    6 ?? /usr/lib/x86_64-linux-gnu/ 0x7ffff7a0918a
    7 ?? /usr/lib/x86_64-linux-gnu/ 0x7ffff7a0ebd4
    8 QMenu::mouseReleaseEvent(QMouseEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff7a1383e
    9 QWidget::event(QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff78d11e8
    10 QMenu::event(QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff7a14053
    11 QApplicationPrivate::notify_helper(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff788db2c
    12 QApplication::notify(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff78938eb
    13 QCoreApplication::notifyInternal(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff6f83c2b

    when I exit ( step to curly brace ), it crashes with

    The inferior stopped because it received a signal from the Operating System.
    Signal name : 
    Signal meaning : 

    and the stack shows
    0 __GI_raise 55 0x7ffff5e97267
    1 __GI_abort 89 0x7ffff5e98eca
    2 __libc_message 175 0x7ffff5edac53
    3 __GI___fortify_fail 38 0x7ffff5f7ae8c
    4 __stack_chk_fail 28 0x7ffff5f7ae30
    5 MainWindow::on_actionPlay_Game_triggered mainwindow.cpp 705 0x416a54
    6 MainWindow::qt_static_metacall moc_mainwindow.cpp 192 0x566b41
    7 MainWindow::qt_metacall moc_mainwindow.cpp 224 0x566c38
    8 QMetaObject::activate(QObject*, int, int, void**) /usr/lib/x86_64-linux-gnu/ 0x7ffff6fb4ae2
    9 QAction::triggered(bool) /usr/lib/x86_64-linux-gnu/ 0x7ffff7883722
    10 QAction::activate(QAction::ActionEvent) /usr/lib/x86_64-linux-gnu/ 0x7ffff7885e28
    11 ?? /usr/lib/x86_64-linux-gnu/ 0x7ffff7a0918a
    12 ?? /usr/lib/x86_64-linux-gnu/ 0x7ffff7a0ebd4
    13 QMenu::mouseReleaseEvent(QMouseEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff7a1383e
    14 QWidget::event(QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff78d11e8
    15 QMenu::event(QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff7a14053
    16 QApplicationPrivate::notify_helper(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff788db2c
    17 QApplication::notify(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff78938eb
    18 QCoreApplication::notifyInternal(QObject*, QEvent*) /usr/lib/x86_64-linux-gnu/ 0x7ffff6f83c2b
    19 QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool) /usr/lib/x86_64-linux-gnu/ 0x7ffff78923da
    20 ?? /usr/lib/x86_64-linux-gnu/ 0x7ffff78e9a43
    ... <More>

  • right this is totally nuts, it's caused by the warning message boxes , 4 of then checking for sprites , sound effects ect. What I did was comment them all out using /**/

        if( Sprites.get_size( ) == 0 )
           QMessageBox::critical(this, tr("Warning"),tr(" No Sprites for game"),QMessageBox::Close );

    Then allowed one through at a time. Then the whole lot. Now it works no crashes :s. Literally all I've done is put in comments compile, uncomment and compile, so it's exactly as it was, but now it works... Out of curiosity can anyone advise how this could happen?

  • Moderators

    This could happen if you had some left-over object files and your compiler didn't recreate them. So when the linker makes the executable it'd be using outdated translation units and creates an executable that crashes. Or you have an error that's intermittently causing the crash (for example buffer that may or may not overwrite memory you don't own depending on the starting/running conditions).

Log in to reply