Как проверить утечки памяти в простом приложении Qt Widget, используя Valgrind? - PullRequest
0 голосов
/ 06 февраля 2019

Я никогда раньше не использовал Valgrind, и сегодня я попытался проверить утечки памяти в простом приложении Qt Widget, сгенерированном Qt Creator.Я создал проект, выполнив следующие шаги:

  1. У меня есть Qt Creator v4.8.1 на основе Qt 5.12.0.В Qt Creator я выбрал следующий пункт меню: Файл -> Новый файл или проект ... -> Приложение -> Приложение Qt Widgets .Затем я нажал Выберите ... -> Далее -> ... -> Готово .Qt Creator сгенерировал следующий исходный код:

main.cpp :

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp :

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.h :

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.ui

<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow" >
  <property name="geometry" >
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle" >
   <string>MainWindow</string>
  </property>
  <widget class="QMenuBar" name="menuBar" />
  <widget class="QToolBar" name="mainToolBar" />
  <widget class="QWidget" name="centralWidget" />
  <widget class="QStatusBar" name="statusBar" />
 </widget>
 <layoutDefault spacing="6" margin="11" />
 <pixmapfunction></pixmapfunction>
 <resources/>
 <connections/>
</ui>

без названия.pro

#-------------------------------------------------
#
# 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
Я скомпилировал этот проект с помощью набора инструментов GCC 8.2.0 в «Режиме отладки» в Ubuntu 18.10.

Затем я попытался запустить скомпилированную программу с помощью следующей команды:

$ valgrind./untitled

Результатом этих операций в Ubuntu 18.10 является:

==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)

Что я делаю не так?

...