0

由于我的 C++ 项目中存在一些(奇怪的)问题,我使用 Visual Leak Detector(第一次)来检查项目的内存泄漏。所以我得到了以下报告:

    WARNING: Visual Leak Detector detected memory leaks!
---------- Block 4 at 0x004D07B0: 200 bytes ----------
  Leak Hash: 0xD2D1B4A0, Count: 1, Total 200 bytes
  Call Stack (TID 8796):
    ucrtbase.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): LASS.exe!operator new() + 0x8 bytes
    clr.dll!0x72D616E5()
  Data:
    28 75 14 03    00 00 00 00    01 00 00 00    00 00 00 00     (u...... ........
    9A 99 99 99    99 99 B9 3F    50 00 00 00    0A 00 00 00     .......? P.......
    00 00 00 00    F4 01 00 00    00 00 00 00    01 00 00 00     ........ ........
    7B 14 AE 47    E1 7A 74 3F    14 00 00 00    BA FF FF FF     {..G.zt? ........
    00 00 00 00    F4 01 00 00    00 00 00 00    01 00 00 00     ........ ........
    7B 14 AE 47    E1 7A 84 3F    00 00 00 00    64 00 00 00     {..G.z.? ....d...
    00 00 00 00    01 00 00 00    14 00 00 00    46 00 00 00     ........ ....F...
    00 00 00 00    64 00 00 00    00 00 00 00    F4 01 00 00     ....d... ........
    01 00 00 00    B8 E2 13 03    F0 AD 18 03    00 00 00 00     ........ ........
    C8 E2 13 03    C8 AB 18 03    00 00 00 00    78 E3 13 03     ........ ....x...
    B8 AC 18 03    00 00 00 00    68 E2 13 03    E8 AC 18 03     ........ h.......
    00 00 00 00    14 00 00 00    01 00 00 00    64 00 00 00     ........ ....d...
    01 00 00 00    00 00 00 00                                   ........ ........


---------- Block 20 at 0x004D0880: 200 bytes ----------
  Leak Hash: 0xD2D1B4A0, Count: 1, Total 200 bytes
  Call Stack (TID 8796):
    ucrtbase.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): LASS.exe!operator new() + 0x8 bytes
    clr.dll!0x72D616E5()
  Data:
    78 74 14 03    00 00 00 00    01 00 00 00    00 00 00 00     xt...... ........
    9A 99 99 99    99 99 B9 3F    50 00 00 00    0A 00 00 00     .......? P.......
    00 00 00 00    F4 01 00 00    00 00 00 00    01 00 00 00     ........ ........
    7B 14 AE 47    E1 7A 74 3F    14 00 00 00    BA FF FF FF     {..G.zt? ........
    00 00 00 00    F4 01 00 00    00 00 00 00    01 00 00 00     ........ ........
    7B 14 AE 47    E1 7A 84 3F    00 00 00 00    64 00 00 00     {..G.z.? ....d...
    00 00 00 00    01 00 00 00    14 00 00 00    46 00 00 00     ........ ....F...
    00 00 00 00    64 00 00 00    00 00 00 00    F4 01 00 00     ....d... ........
    01 00 00 00    38 E2 13 03    00 F0 15 03    00 00 00 00     ....8... ........
    B8 E1 13 03    88 00 7F 05    00 00 00 00    08 E2 13 03     ........ ........
    20 FF 7E 05    00 00 00 00    E8 E1 13 03    80 FF 7E 05     ..~..... ......~.
    00 00 00 00    14 00 00 00    01 00 00 00    64 00 00 00     ........ ....d...
    01 00 00 00    00 00 00 00                                   ........ ........


---------- Block 31 at 0x0053E1B8: 72 bytes ----------
  Leak Hash: 0x3F88029B, Count: 1, Total 72 bytes
  Call Stack (TID 8796):
    ucrtbase.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): LASS.exe!operator new() + 0x8 bytes
    clr.dll!0x72D616E5()
  Data:
    60 BC 55 00    40 3E 80 05    A0 3F 80 05    A0 3F 80 05     `.U.@>.. .?...?..
    60 BB 55 00    20 34 18 03    00 00 00 00    00 00 00 00     `.U..4.. ........
    00 00 00 00    20 00 00 00    2F 00 00 00    80 BC 55 00     ........ /.....U.
    00 2E 18 03    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    20 00 00 00    2F 00 00 00                                   ..../... ........


---------- Block 33 at 0x0055BB60: 8 bytes ----------
  Leak Hash: 0xA49C5AA6, Count: 1, Total 8 bytes
  Call Stack (TID 8796):
    ucrtbase.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): LASS.exe!operator new() + 0x8 bytes
    clr.dll!0x72D616E5()
  Data:
    C8 E1 53 00    00 00 00 00                               

   ..S..... ........

    //And many more...

可惜我不明白,VLD 想说的是问题所在。双击“f:\dd...”行,它应该将我的课程设置到有问题的行,不是吗?但它没有。

我现在的问题是:我如何找到问题所在的区域,或者换句话说“我如何阅读这些报告”?

此外:

  • 我使用 Visual Studio 2015
  • 该项目是一个 C++ Windows 窗体项目
  • 我将 vld.h 包含在附加包含中,并将 lib 目录包含在项目的附加库中
  • main()我使用#include <vld.h>and _CrtDumpMemoryLeaks();

编辑:

我的主要(简化版本,但提供类似的报告):

//some class-includes
#include <vld.h> 

using namespace System;
using namespace System::Windows::Forms;
using namespace std; 

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

[STAThread]
void Main()
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);


    Experiment* experiment = new Experiment();
    Experiment_List*  running_experiments = new Experiment_List(); 

    while(!experiment->end) {
        experiment= new Experiment();

        LASS::MainWindow form(experiment, running_experiments);
        form.ShowDialog();

        if(!experiment->end){
            running_experiments->register_experiment(experiment); 
        }
    }

    running_experimente->end_all(); 

    _CrtDumpMemoryLeaks();

    exit(0); 
}

不幸的是,大约有 40 个课程,我不想发布...

4

1 回答 1

0

我不知道问题究竟出在哪里。对我来说,它有助于在 RELEASE 模式下运行程序,而不是在 DEBUG 模式下运行。

我想,我的问题是同时处理托管和非托管代码。我在托管代码中有非托管代码。似乎 CLR 在调试模式下使用了不同的新运算符。不符合 c++ 标准。

根据:Using push_back() for STL List in C++ 导致访问冲突,崩溃

如果您 malloc() 一个 C++ 类,则不会为该类的任何字段调用构造函数

VS 将进入类new_scalar.cpp中的构造函数。人们说这取决于视觉泄漏检测器 (VLD)。您在包含中使用它们。

最后,尝试区分您的代码

#pragma managed

#pragma unmanaged

并以RELEASE模式运行。

于 2016-10-04T22:01:07.743 回答