title: Exit Application in Qt
body: <p>I have built an app in Qt that contains two buttons: an exit button and an import button. When the import button is pushed, a list of buttons is shown in a scrollarea on the screen (the file loggers.csv contains the data 1;2;3;4;5;). </p>
  
  <p>It all works fine, but when I push the exit button (which of course should close everything), the app is not stopped properly (the stop button of Qt is still active, and the play button isn't). When I run the debugger and push the exit button it gives an error: Invalid address specified to RtlFreeHeap( 0ADF0000, 0028FE40 ). Can anybody help me?</p>
  
  <p><strong>main</strong></p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include "mainwindow.h"
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
  
      MainWindow w;
  
      w.showFullScreen();
  
      return a.exec();
  }
  </code></pre>
  
  <p><strong>Mainwindow.h</strong></p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;QtGui&gt;
  #include "logger.h"
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
      QPushButton exit_btn;
      QPushButton import_btn;
  
  private slots:
  
      void createMenus();
      void exit();
      void import();
  
  private:
  
      int window_width;
      int window_height;
  
      int numLoggers;
      int numSelected;
  
      QVector&lt;Logger*&gt; loggers;
  
      QScrollArea * scroll_area;
  
      QVBoxLayout scrollLayout;
  
      QWidget viewport;
  
      Ui::MainWindow *ui;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p><strong>Mainwindow.cpp:</strong></p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  #include "QtGui"
  
  MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  
      window_width = QApplication::desktop()-&gt;width();
      window_height = QApplication::desktop()-&gt;height();
  
      createMenus();
  
      connect(&amp;exit_btn,SIGNAL(clicked()),this,SLOT(exit()));
      connect(&amp;import_btn,SIGNAL(clicked()),this,SLOT(import()));
  }
  
  MainWindow::~MainWindow()
  {
     delete ui;
  }
  
  void MainWindow::createMenus()
  {
      import_btn.setParent(ui-&gt;centralWidget);
      import_btn.setGeometry(400,300,100,100);
      import_btn.setText("IMPORT");
  
      exit_btn.setText("EXIT");
      exit_btn.setParent(ui-&gt;centralWidget);
      exit_btn.setGeometry(window_width-50,12,32,32);
  
      viewport.setLayout(&amp;scrollLayout);
      viewport.resize(0,0);
  
      scroll_area = new QScrollArea(ui-&gt;centralWidget);
      scroll_area-&gt;setGeometry(0,66,317,window_height-116);
      scroll_area-&gt;setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      scroll_area-&gt;setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      scroll_area-&gt;setWidget(&amp;viewport);
      scroll_area-&gt;setGeometry(0,97,317,window_height-228);
  
      scrollLayout.setMargin(0);
      scrollLayout.setSpacing(0);
  }
  
  void MainWindow::exit()
  {
      close();
      qApp-&gt;quit();
  }
  
  void MainWindow::import()
  {
      numSelected=0;
  
      QFile f("Loggers3.csv");
  
      if (f.open(QIODevice::ReadOnly))
      {
          numLoggers=0;
  
          QString data;
          data = f.readAll();
          QStringList vals = data.split(';');
  
          while(vals.size()&gt;=1)
          {
              Logger * logger = new Logger;
  
              logger-&gt;setNumber(vals[0].toInt());
              vals.removeAt(0);
  
              loggers&lt;&lt;logger;
  
              numLoggers++;
          }
          f.close();
  
  
          for(int i=0; i&lt;numLoggers;i++)
          {
              loggers[i]-&gt;createButtons();
              scrollLayout.addWidget(loggers[i]-&gt;button);
          }
  
          viewport.resize(367,numLoggers*60);
      }
  }
  </code></pre>
  
  <p><strong>logger.h</strong></p>
  
  <pre><code>#ifndef LOGGER_H
  #define LOGGER_H
  
  #include &lt;QtGui&gt;
  
  class Logger : public QWidget
  {
      Q_OBJECT
  public:
      explicit Logger(QWidget *parent = 0);
  
      ~Logger();
      int number;
      QLabel num;
      QToolButton * button;
      bool checked;
  
  signals:
  
  public slots:
  
      void setNumber(int number);
      void createButtons();
  };
  
  #endif // LOGGER_H
  </code></pre>
  
  <p><strong>logger.cpp</strong></p>
  
  <pre><code>#include "logger.h"
  #include &lt;QtGui&gt;
  
  Logger::Logger(QWidget *parent) :
      QWidget(parent)
  {
      button = new QToolButton;
      button-&gt;setCheckable(true);
      button-&gt;setMinimumSize(317,60);
      button-&gt;setStyleSheet("QToolButton{background-image: url(images/btn_bg); border:none}");
  }
  
  Logger::~Logger()
  {
  }
  
  void Logger::setNumber(int logNumber)
  {
      number=logNumber;
  }
  
  void Logger::createButtons()
  {
      QLayout * layout = new QHBoxLayout;
  
      QSpacerItem *spacer = new QSpacerItem(120, 31, QSizePolicy::Maximum, SizePolicy::Maximum);
  
      num.setStyleSheet("color: white; font: bold 16px");
      num.setText(QString::number(number));
  
      layout-&gt;addWidget(&amp;num);
      layout-&gt;addItem(spacer);
  
      button-&gt;setLayout(layout);
  }
  </code></pre>