【问题标题】:Using Visual Leak Detector使用视觉泄漏检测器
【发布时间】:2013-03-02 20:05:20
【问题描述】:

我的 C++ 代码会随机崩溃。在代码中我使用了一些 Qt 库。编译器是 MSVS 2010。根据一些可能是由于内存泄漏的建议,我开始使用 Visual Leak Detector 进行查找。我对 Visual Leak Detector 和内存泄漏检测非常陌生。

这是main.cpp中的代码:

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

而且MainWindow 构造函数看起来像这样:

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    ,   m_Inputdevice(QAudioDeviceInfo::defaultInputDevice())
    ,   m_audioInput(0)
    ,   m_input(0)
    ,   m_iVolume(0)
    ,   m_buffer(BufferSize, 0)
    ,   ui(new Ui::MainWindow)
{  // LINE 23
    ui->setupUi(this);
    initializeAudio(); // LINE 26
    udpSocket = new QUdpSocket(this); 
    udpSocket1 = new QUdpSocket(this);
    ...
    char* str = new char [30]; 
    VLDEnable();
    VLDReportLeaks(); 
}

而析构函数是:

MainWindow::~MainWindow()
{
  delete udpSocket;
  delete udpSocket1;
  delete m_audioInput;
  delete ui;
}

根据一些建议,我刚刚安装它并得到以下结果:

WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00CDED68: 12 bytes ----------
Call Stack:
c:\tmp\mainwindow.cpp (23): TestProgram.exe!MainWindow::MainWindow + 0x59 bytes
c:\tmp\main.cpp (7): TestProgram.exe!main + 0xA bytes
c:\myqt\4.8.2\src\winmain\qtmain_win.cpp (131): TestProgram.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): TestProgram.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): TestProgram.exe!WinMainCRTStartup
0x756233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
C8 F9 CD 00    38 FD CD 00    28 3B 6E 02                    ....8... (;n.....



---------- Block 2 at 0x00CDF9C8: 20 bytes ----------
Call Stack:
c:\tmp\generatedfiles\ui_mainwindow.h (36): TestProgram.exe!Ui_MainWindow::setupUi + 0x7 bytes
c:\tmp\mainwindow.cpp (26): TestProgram.exe!MainWindow::MainWindow
c:\tmp\main.cpp (7): TestProgram.exe!main + 0xA bytes
c:\myqt\4.8.2\src\winmain\qtmain_win.cpp (131): TestProgram.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): TestProgram.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): TestProgram.exe!WinMainCRTStartup
0x756233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
5C 90 DA 00    18 FA CD 00    38 90 DA 00    00 00 CD CD     \....... 8.......
CC FA CD 00                                                  ........ ........


---------- Block 3 at 0x00CDFD38: 20 bytes ----------
Call Stack:
c:\tmp\generatedfiles\ui_mainwindow.h (38): TestProgram.exe!Ui_MainWindow::setupUi + 0x7 bytes
c:\tmp\mainwindow.cpp (26): TestProgram.exe!MainWindow::MainWindow
c:\tmp\main.cpp (7): TestProgram.exe!main + 0xA bytes
c:\myqt\4.8.2\src\winmain\qtmain_win.cpp (131): TestProgram.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): TestProgram.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): TestProgram.exe!WinMainCRTStartup
0x756233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
24 8F DA 00    88 FD CD 00    00 8F DA 00    00 00 CD CD     $....... ........
3C FE CD 00                                                  <....... ........


---------- Block 4 at 0x026E3B28: 20 bytes ----------
Call Stack:
c:\tmp\generatedfiles\ui_mainwindow.h (41): TestProgram.exe!Ui_MainWindow::setupUi + 0x7 bytes
c:\tmp\mainwindow.cpp (26): TestProgram.exe!MainWindow::MainWindow
c:\tmp\main.cpp (7): TestProgram.exe!main + 0xA bytes
c:\myqt\4.8.2\src\winmain\qtmain_win.cpp (131): TestProgram.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): TestProgram.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): TestProgram.exe!WinMainCRTStartup
0x756233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
24 8F DA 00    78 3B 6E 02    00 8F DA 00    00 00 CD CD     $...x;n. ........
2C 3C 6E 02                                                  ,<n..... ........


---------- Block 6 at 0x026E4200: 8 bytes ----------
Call Stack:
c:\tmp\mainwindow.cpp (28): TestProgram.exe!MainWindow::MainWindow + 0x7 bytes
c:\tmp\main.cpp (7): TestProgram.exe!main + 0xA bytes
c:\myqt\4.8.2\src\winmain\qtmain_win.cpp (131): TestProgram.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): TestProgram.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): TestProgram.exe!WinMainCRTStartup
0x756233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
34 8D DA 00    50 49 6E 02                                   4...PIn. ........


---------- Block 5 at 0x026E4C90: 12 bytes ----------
Call Stack:
c:\tmp\mainwindow.cpp (76): TestProgram.exe!MainWindow::createAudioInput + 0x7 bytes
c:\tmp\mainwindow.cpp (67): TestProgram.exe!MainWindow::initializeAudio
c:\tmp\mainwindow.cpp (28): TestProgram.exe!MainWindow::MainWindow
c:\tmp\main.cpp (7): TestProgram.exe!main + 0xA bytes
c:\myqt\4.8.2\src\winmain\qtmain_win.cpp (131): TestProgram.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): TestProgram.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): TestProgram.exe!WinMainCRTStartup
0x756233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
74 91 DA 00    A0 4B 6E 02    78 50 6E 02                    t....Kn. xPn.....


---------- Block 7 at 0x026E54C8: 8 bytes ----------
Call Stack:
c:\tmp\mainwindow.cpp (29): TestProgram.exe!MainWindow::MainWindow + 0x7 bytes
c:\tmp\main.cpp (7): TestProgram.exe!main + 0xA bytes
c:\myqt\4.8.2\src\winmain\qtmain_win.cpp (131): TestProgram.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): TestProgram.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): TestProgram.exe!WinMainCRTStartup
0x756233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
34 8D DA 00    10 55 6E 02                                   4....Un. ........


---------- Block 8 at 0x026E62D8: 30 bytes ----------
Call Stack:
c:\tmp\mainwindow.cpp (33): TestProgram.exe!MainWindow::MainWindow + 0x7 bytes
c:\tmp\main.cpp (7): TestProgram.exe!main + 0xA bytes
c:\myqt\4.8.2\src\winmain\qtmain_win.cpp (131): TestProgram.exe!WinMain + 0x12 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (547): TestProgram.exe!__tmainCRTStartup + 0x2C bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): TestProgram.exe!WinMainCRTStartup
0x756233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77139EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77139EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD           ........ ........

感谢任何帮助,以根据这些结果找出内存泄漏的来源。

【问题讨论】:

  • 如果能看到 mainwindow.cpp 和 main.cpp 中的代码就好了
  • 我在main.cpp中添加了代码,将在maindindow.cpp中删减代码并发布。
  • 请同时显示MainWindow 构造函数,并请标出第23 和26 行(消息中指出的行)。另外,请注意 Qt 可能会分配用于整个程序的内存,因此不会被释放。这看起来像是内存泄漏,但实际上并非如此。
  • @JoachimPileborg 好的,我会在问题中展示它们。
  • @TJ1 你释放了析构函数中的所有分配吗?此外,在 intializeAudio()) 中进行的所有分配都需要释放

标签: c++ visual-studio qt memory-leaks visual-leak-detector


【解决方案1】:

检查main.cpp文件的代码 -- 有一些错误。

有一条规则——每个new 都必须有它的delete

在您的构造函数中,奇怪的变量 char* str = new char [30]; 被声明、分配,但从未被释放。

m_buffer(BufferSize, 0)m_Inputdevice(QAudioDeviceInfo::defaultInputDevice()) 是什么?

【讨论】:

  • 谢谢你的回答,其实你是对的str应该被释放,你也指出了正确的方向,原来主要原因是BufferSize,它接近边界我增加了一点,现在没有崩溃。
  • 太好了!乐于助人。小心你的所有变量!祝你好运!
【解决方案2】:

如果您设置了父对象,请不要显式删除您的 Qt 对象。

QObjects organize themselves in object trees。将 Qt 小部件等作为父级,将小部件的所有权转移给该父级。销毁后,父母将负责清理其孩子。在这些情况下,您的new 不需要匹配的delete。事实上,如果你这样做,就会给你带来麻烦。

至少你不需要

delete udpSocket;
delete udpSocket1;

根据您的其他代码,您甚至可能不需要

delete m_audioInput;
delete ui;

【讨论】:

  • 谢谢巴特。 m_audioInput 定义为:m_audioInput = new QAudioInput(m_Inputdevice, m_format, this); 所以你认为我不应该删除它?
  • 你的 this is the parent 在这种情况下。那现在是您的 QAudioInput 的“所有者”。所以不,你不应该删除它。
猜你喜欢
  • 1970-01-01
  • 2011-12-13
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
相关资源
最近更新 更多