Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. System restarts , after closing Qt Application

System restarts , after closing Qt Application

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dcqt
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dcqt
      wrote on last edited by
      #2

      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
      }
      @

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

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

        (Z(:^

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dcqt
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            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);
            @

            (Z(:^

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dcqt
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                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.

                (Z(:^

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dcqt
                  wrote on last edited by
                  #8

                  [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.

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    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.

                    (Z(:^

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dcqt
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved