title: How to check memory leaks in simple Qt Widget application using Valgrind?
body: <p>I never use Valgrind before and today I tried to check memory leaks in simple Qt Widget application generated by Qt Creator. I created project by following steps:</p>
  
  <ol>
  <li>I've got Qt Creator v4.8.1 based on Qt 5.12.0. In Qt Creator I chose following menu item: <strong>File -> New File or Project... -> Application -> Qt Widgets Application</strong>. Then I pressed <strong>Choose... -> Next -> ... -> Finish</strong>. Qt Creator generated following source code:</li>
  </ol>
  
  <p><strong>main.cpp</strong>:</p>
  
  <pre><code>#include "mainwindow.h"
  #include &lt;QApplication&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p><strong>mainwindow.cpp</strong>:</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  </code></pre>
  
  <p><strong>mainwindow.h</strong>:</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = nullptr);
      ~MainWindow();
  
  private:
      Ui::MainWindow *ui;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p><strong>mainwindow.ui</strong></p>
  
  <pre><code>&lt;ui version="4.0"&gt;
   &lt;class&gt;MainWindow&lt;/class&gt;
   &lt;widget class="QMainWindow" name="MainWindow" &gt;
    &lt;property name="geometry" &gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;400&lt;/width&gt;
      &lt;height&gt;300&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="windowTitle" &gt;
     &lt;string&gt;MainWindow&lt;/string&gt;
    &lt;/property&gt;
    &lt;widget class="QMenuBar" name="menuBar" /&gt;
    &lt;widget class="QToolBar" name="mainToolBar" /&gt;
    &lt;widget class="QWidget" name="centralWidget" /&gt;
    &lt;widget class="QStatusBar" name="statusBar" /&gt;
   &lt;/widget&gt;
   &lt;layoutDefault spacing="6" margin="11" /&gt;
   &lt;pixmapfunction&gt;&lt;/pixmapfunction&gt;
   &lt;resources/&gt;
   &lt;connections/&gt;
  &lt;/ui&gt;
  </code></pre>
  
  <p><strong>untitled.pro</strong></p>
  
  <pre><code>#-------------------------------------------------
  #
  # Project created by QtCreator 2019-02-06T15:14:11
  #
  #-------------------------------------------------
  
  QT       += core gui
  
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  
  TARGET = untitled
  TEMPLATE = app
  
  # The following define makes your compiler emit warnings if you use
  # any feature of Qt which has been marked as deprecated (the exact warnings
  # depend on your compiler). Please consult the documentation of the
  # deprecated API in order to know how to port your code away from it.
  DEFINES += QT_DEPRECATED_WARNINGS
  
  # You can also make your code fail to compile if you use deprecated APIs.
  # In order to do so, uncomment the following line.
  # You can also select to disable deprecated APIs only up to a certain version of Qt.
  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  
  CONFIG += c++11
  
  SOURCES += \
          main.cpp \
          mainwindow.cpp
  
  HEADERS += \
          mainwindow.h
  
  FORMS += \
          mainwindow.ui
  
  # Default rules for deployment.
  qnx: target.path = /tmp/$${TARGET}/bin
  else: unix:!android: target.path = /opt/$${TARGET}/bin
  !isEmpty(target.path): INSTALLS += target
  </code></pre>
  
  <ol start="2">
  <li>I compiled this project by GCC 8.2.0 toolchain in 'Debug mode' on Ubuntu 18.10.</li>
  <li><p>Then i tried to run compiled program by following command:</p>
  
  <p>$ valgrind ./untitled</p></li>
  </ol>
  
  <p>Result of this operations on Ubuntu 18.10 is:</p>
  
  <pre><code>==12228== Memcheck, a memory error detector
  ==12228== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
  ==12228== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
  ==12228== Command: ./untitled
  ==12228== 
  ==12228== 
  ==12228== Process terminating with default action of signal 11 (SIGSEGV)
  ==12228==  General Protection Fault
  ==12228==    at 0x15C006A7: ??? (in /usr/lib/x86_64-linux-gnu/dri/vmwgfx_dri.so)
  ==12228==    by 0x15C0DBC5: ??? (in /usr/lib/x86_64-linux-gnu/dri/vmwgfx_dri.so)
  ==12228==    by 0x15C0DFEB: ??? (in /usr/lib/x86_64-linux-gnu/dri/vmwgfx_dri.so)
  ==12228==    by 0x154B3172: ??? (in /usr/lib/x86_64-linux-gnu/dri/vmwgfx_dri.so)
  ==12228==    by 0x159AF457: ??? (in /usr/lib/x86_64-linux-gnu/dri/vmwgfx_dri.so)
  ==12228==    by 0x1587E5B1: ??? (in /usr/lib/x86_64-linux-gnu/dri/vmwgfx_dri.so)
  ==12228==    by 0x1587984D: ??? (in /usr/lib/x86_64-linux-gnu/dri/vmwgfx_dri.so)
  ==12228==    by 0x14F8BD3D: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
  ==12228==    by 0x14F63C52: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
  ==12228==    by 0x14F5F323: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
  ==12228==    by 0x14F5FD3C: ??? (in /usr/lib/x86_64-linux-gnu/libGLX_mesa.so.0.0.0)
  ==12228==    by 0x14CFECF1: QXcbGlxWindow::createVisual() (in /opt/Software/Qt/5.12.0/gcc_64/plugins/xcbglintegrations/libqxcb-glx-integration.so)
  ==12228== 
  ==12228== HEAP SUMMARY:
  ==12228==     in use at exit: 3,561,397 bytes in 44,679 blocks
  ==12228==   total heap usage: 347,392 allocs, 302,713 frees, 29,095,722 bytes allocated
  ==12228== 
  ==12228== LEAK SUMMARY:
  ==12228==    definitely lost: 14,216 bytes in 80 blocks
  ==12228==    indirectly lost: 4,334 bytes in 203 blocks
  ==12228==      possibly lost: 28,587 bytes in 457 blocks
  ==12228==    still reachable: 3,448,988 bytes in 43,405 blocks
  ==12228==                       of which reachable via heuristic:
  ==12228==                         length64           : 3,376 bytes in 61 blocks
  ==12228==                         newarray           : 2,048 bytes in 48 blocks
  ==12228==         suppressed: 0 bytes in 0 blocks
  ==12228== Rerun with --leak-check=full to see details of leaked memory
  ==12228== 
  ==12228== For counts of detected and suppressed errors, rerun with: -v
  ==12228== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 4)
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>What am I doing wrong?</p>