System restarts , after closing Qt Application



  • Hi,
    I observed a strange problem today.
    I run one of my applications using QtCreator.
    Immediately after closing the application my system restarts.
    if i run same through terminal it does not happen.
    even if i double click on the exe the restart is happening.

    i am using QtCreator 2.6.1 with Qt 5.0.1 on ubuntu 11.10

    any body ever faced this issue.

    i don't have a clue , why thats happening.

    forgot to mention , its not like actual restart, it does not take normal restart time, just closes all opened windows and gives me login window immediately.



  • I found the problem.

    some where in the code its done as below.

    if comment adding keypad to main window and remove
    *delete remote * from destructor , no restart occurs,
    but i want that code to be present , dont understand what exactly the probelm.

    please any clues... help ....

    @
    RemoteControl::~RemoteControl()
    {
    qDebug()<<"From Remote Destructor ";
    if (NULL != m_pRemote){
    delete m_pRemote;
    qDebug()<<"m_pRemote freed";
    m_pRemote = NULL;
    }
    }
    @

    @
    RemoteControl::RemoteControl(int p_nRemoteWidth ,int p_nRemoteHeight,QWidget* parent) :
    QWidget(parent),m_nRemoteWidth(p_nRemoteWidth),m_nRemoteHeight(p_nRemoteHeight)
    {
    setFixedSize(p_nRemoteWidth,p_nRemoteHeight);
    }
    @

    @
    QWidget* RemoteControl::createRemoteControl(){
    m_pRemote = new QWidget(this);
    for(int i=0;i<NO_OF_BUTTONS;i++)
    {
    m_nBtnMap[i]=btn[i];
    m_pButton[i]= new QPushButton(m_pRemote);
    m_pSignalMapper = new QSignalMapper (this) ;
    connect (m_pButton[i], SIGNAL(released()), m_pSignalMapper, SLOT(map())) ;
    m_pSignalMapper -> setMapping (m_pButton[i], i) ;
    connect (m_pSignalMapper, SIGNAL(mapped(int)), this, SLOT(BtnHandler(int))) ;
    }
    return this;
    }
    @

    and in the mainwindow

    @
    mainwindow = new QWidget();
    setCentralWidget(mainwindow);
    mainLayout = new QHBoxLayout();
    mainwindow->setLayout(mainLayout);

    containerWidget = new QWidget();
    containerLayout = new QVBoxLayout();

    remote = new RemoteControl(REMOTE_WIDTH,REMOTE_HEIGHT);
    keypad = remote->createRemoteControl();
    containerWidget->setLayout(containerLayout);
    containerLayout->addWidget(display);

     mainLayout->addWidget(containerWidget)
    

    //mainLayout->addWidget(keypad); this only creates all the problem ,
    @

    @
    MainWindow::~MainWindow()
    {
    if(NULL != appwindow) {
    delete appwindow;
    qDebug()<<"appwindow freed";
    appwindow = NULL;
    display = NULL;
    }

    if(NULL != containerLayout){
        delete containerLayout;
        qDebug()<<"containerLayout freed";
       containerLayout = NULL;
    }
    
    if(NULL != containerWidget){
        delete containerWidget;
        qDebug()<<"containerWidget freed";
       containerWidget = NULL;
    }
    
    if(NULL != mainLayout){
         delete mainLayout;
          qDebug()<<"mainLayout freed";
        mainLayout = NULL;
     }
    

    #if 0
    this particular code is giving segmentation fault or making system restrart
    qDebug()<<"Remote"<<remote;
    if(NULL != remote){
    qDebug()<<"In remote ";
    delete remote;
    qDebug()<<"remote freed";
    remote = NULL;
    keypad = NULL;
    }

    if(NULL != mainwindow){
        delete mainwindow;
        qDebug()<<"mainwindow freed";
       mainwindow = NULL;
    }
    

    #endif
    }
    @


  • Moderators

    Don't delete QObjects yourself, the meta object system will do that for you.



  • So, deleting all these widgets in destructor is not necessary.??
    my doubt is why only particular widget it is giving error when rest all working fine.


  • Moderators

    As long as you set the parent correctly, you don't need to delete them in the destructor. But you are not setting the parent, and that may be the source of your problem. Do this:
    @
    mainwindow = new QWidget(this);
    setCentralWidget(mainwindow);
    mainLayout = new QHBoxLayout(this);
    mainwindow->setLayout(mainLayout);

    containerWidget = new QWidget(this);
    containerLayout = new QVBoxLayout(this);
    @



  • Ok, i have to try this.
    So , now deleting the QMainwindow will delete all of its children.?
    is that required or its also done by MOS.


  • Moderators

    Please read QObject documentation :)

    In Qt, QObjects (all Widgets are also QObjects!) are put into a parent-child hierarchy. When you delete a parent object, it will automatically delete all it's children.



  • [quote author="sierdzio" date="1382635133"]As long as you set the parent correctly, you don't need to delete them in the destructor. But you are not setting the parent, and that may be the source of your problem. Do this:

    @
    mainLayout = new QHBoxLayout(this);
    containerLayout = new QVBoxLayout(this);
    @
    [/quote]
    this gives warning "Already has layout on parent"

    I used this on every widget except on layouts, and have my delete in the destructor , now i am not getting any memory problem.

    now new question arises :
    if the objects are automatically deleted, deleting in destructor should give segmentation or dangling , why is it not happening.


  • Moderators

    That depends on the compiler you are using. I don't know what Qt does exactly to the objects it is deleting, but apparently it makes delete behave decently most of the time ;)

    Or it probably is the other way around: Qt is trying to delete after you have deleted the stuff in the destructor, and since it encounters NULL pointers, it skips deletion.

    This is all speculation, though, I would need to check Qt source code to be sure what happens.



  • Thats what making me confuse !!

    I guess , i need to read a lot on this memory cleanup in Qt.

    Thanks a lot sierdzio for your time.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.