1

我有一个很大的内存泄漏,我需要一些帮助来找出它的来源。首先,关于代码的一些理论:

这是一个 Kinect 项目。它是在 MS Visual Studio 2012 中构建的 MFC 项目,它使用 Direct2D 将图像放在屏幕上。当我从 Kinect 1 切换到 Kinect 2 时会出现此问题。然后重建除“主”对象之外的每个对象(解构/释放并设置为 NULL(如果指针))。这包括:

  • 所有 Direct2D 对象(工厂、渲染目标、位图、画笔、一切)
  • 带有线程的 Kinect 面部跟踪器
  • 带有线程的 kinect 对象。

对于每个 new 关键字,我在析构函数中使用了 delete 关键字(所以我没有做任何事情来破坏没有用 new 初始化的指针)。我关闭了所有线程处理程序并将它们设置为 NULL。

现在我有内存泄漏,我需要一些帮助才能找到它。我从使用MS MFC 工具开始

初始化后我有一些输出:

Dumping objects ->
{402} normal block at 0x1531E8F0, 28 bytes long.
 Data: <<       @       > 3C 07 06 00 F0 00 00 00 40 01 00 00 00 05 00 00 
{401} normal block at 0x152F6CD8, 96 bytes long.
 Data: <    >           > 00 CD CD CD 3E 07 06 00 D0 04 00 00 D8 17 00 00 
{400} normal block at 0x03EA0040, 1229064 bytes long.
 Data: <R            E, > 52 07 06 00 B8 A9 13 00 D0 87 D0 00 18 45 2C 15 
{396} normal block at 0x152C9EA0, 20 bytes long.
 Data: <                > 80 F9 89 00 01 00 00 00 01 00 00 00 01 00 00 00 
c:\users\work\documents\github\kinectmain\testmfc\main.cpp(148) : {305} client block at 0x152CF618, subtype c0, 8 bytes long.
a CFont object at $152CF618, 8 bytes long
{295} normal block at 0x152CFC20, 24 bytes long.
 Data: <X   ( , X       > 58 07 CC 00 28 F8 2C 15 58 07 CC 00 00 00 CD CD 
{284} normal block at 0x152CF828, 24 bytes long.
 Data: <X   X     ,     > 58 07 CC 00 58 07 CC 00 20 FC 2C 15 01 00 CD CD 
{206} normal block at 0x00CD0588, 12 bytes long.
 Data: <    @       > B0 04 CD 00 40 05 CD 00 B8 A9 13 00 
{205} normal block at 0x00CD0540, 12 bytes long.
 Data: <            > 88 05 CD 00 B0 04 CD 00 18 BE 13 00 
{204} normal block at 0x00CD04F8, 8 bytes long.
 Data: <        > EC F3 89 00 00 00 00 00 
{203} normal block at 0x00CD04B0, 12 bytes long.
 Data: <@           > 40 05 CD 00 88 05 CD 00 CD CD CD CD 
{202} normal block at 0x00CD0468, 12 bytes long.
 Data: <    H       > 08 FE CC 00 48 A5 CC 00 18 BE 13 00 
{201} normal block at 0x00CCFE08, 12 bytes long.
 Data: <H   h       > 48 A5 CC 00 68 04 CD 00 B8 A9 13 00 
{200} normal block at 0x00CCA590, 8 bytes long.
 Data: <        > FC A4 CC 00 00 00 00 00 
{199} normal block at 0x00CCA548, 12 bytes long.
 Data: <h           > 68 04 CD 00 08 FE CC 00 CD CD CD CD 
c:\users\work\documents\github\kinectmain\testmfc\main.cpp(163) : {198} normal block at 0x00CCA4F8, 16 bytes long.
 Data: <R       H       > 52 07 06 00 90 A5 CC 00 48 A5 CC 00 02 00 00 00 
{188} normal block at 0x00CC07B0, 8 bytes long.
 Data: <        > 9C FB A1 00 00 00 00 00 
{187} normal block at 0x00CC0758, 24 bytes long.
 Data: <( , ( ,   ,     > 28 F8 2C 15 28 F8 2C 15 20 FC 2C 15 01 01 CD CD 
Object dump complete.
0 bytes in 0 Free Blocks.
1229392 bytes in 17 Normal Blocks.
13498 bytes in 73 CRT Blocks.
0 bytes in 0 Ignore Blocks.
8 bytes in 1 Client Blocks.
Largest number used: 1245916 bytes.
Total allocations: 2504946 bytes.

在 kinect 切换之后:

Dumping objects ->
{961} normal block at 0x1531E8F0, 28 bytes long.
 Data: <<       @       > 3C 07 06 00 F0 00 00 00 40 01 00 00 00 05 00 00 
{960} normal block at 0x1534A658, 96 bytes long.
 Data: <    >   p       > 00 CD CD CD 3E 07 06 00 70 05 00 00 A8 15 00 00 
{959} normal block at 0x03EA0040, 1229064 bytes long.
 Data: <R         ,   4 > 52 07 06 00 18 BE 13 00 90 F1 2C 15 98 9B 34 15 
{956} normal block at 0x152CBFD0, 20 bytes long.
 Data: <                > 80 F9 89 00 01 00 00 00 01 00 00 00 01 00 00 00 
c:\users\work\documents\github\kinectmain\testmfc\main.cpp(148) : {305} client block at 0x152CF618, subtype c0, 8 bytes long.
a CFont object at $152CF618, 8 bytes long
{295} normal block at 0x152CFC20, 24 bytes long.
 Data: <X   ( , X       > 58 07 CC 00 28 F8 2C 15 58 07 CC 00 00 00 CD CD 
{284} normal block at 0x152CF828, 24 bytes long.
 Data: <X   X     ,     > 58 07 CC 00 58 07 CC 00 20 FC 2C 15 01 00 CD CD 
{206} normal block at 0x00CD0588, 12 bytes long.
 Data: <    @       > B0 04 CD 00 40 05 CD 00 B8 A9 13 00 
{205} normal block at 0x00CD0540, 12 bytes long.
 Data: <            > 88 05 CD 00 B0 04 CD 00 18 BE 13 00 
{204} normal block at 0x00CD04F8, 8 bytes long.
 Data: <        > EC F3 89 00 00 00 00 00 
{203} normal block at 0x00CD04B0, 12 bytes long.
 Data: <@           > 40 05 CD 00 88 05 CD 00 CD CD CD CD 
{202} normal block at 0x00CD0468, 12 bytes long.
 Data: <    H       > 08 FE CC 00 48 A5 CC 00 18 BE 13 00 
{201} normal block at 0x00CCFE08, 12 bytes long.
 Data: <H   h       > 48 A5 CC 00 68 04 CD 00 B8 A9 13 00 
{200} normal block at 0x00CCA590, 8 bytes long.
 Data: <        > FC A4 CC 00 00 00 00 00 
{199} normal block at 0x00CCA548, 12 bytes long.
 Data: <h           > 68 04 CD 00 08 FE CC 00 CD CD CD CD 
c:\users\work\documents\github\kinectmain\testmfc\main.cpp(163) : {198} normal block at 0x00CCA4F8, 16 bytes long.
 Data: <R       H       > 52 07 06 00 90 A5 CC 00 48 A5 CC 00 02 00 00 00 
{188} normal block at 0x00CC07B0, 8 bytes long.
 Data: <        > 9C FB A1 00 00 00 00 00 
{187} normal block at 0x00CC0758, 24 bytes long.
 Data: <( , ( ,   ,     > 28 F8 2C 15 28 F8 2C 15 20 FC 2C 15 01 01 CD CD 
Object dump complete.
0 bytes in 0 Free Blocks.
1229392 bytes in 17 Normal Blocks.
13498 bytes in 73 CRT Blocks.
0 bytes in 0 Ignore Blocks.
8 bytes in 1 Client Blocks.
Largest number used: 1245916 bytes.
Total allocations: 3742634 bytes.

所以总分配量从 2504946 字节增加到 3742634 字节,但没有其他变化。

此外,每次开关我都会收到以下错误:

calling DestroyWindow in CWnd::~CWnd; OnDestroy or PostNcDestroy in derived class will not be called.

即使在程序关闭之前我不会调用destroyWindow。

关于可能是什么原因或我可以使用什么工具来查找原因的任何提示、建议或提示都会很棒。

4

2 回答 2

6

{ } 括号中的数字是分配订单号。如果你的程序是完全确定的并且每次都做同样的事情,那么这些数字应该从一次运行到下一次保持不变。

在这种情况下,您可以使用此技巧。将以下代码添加到 main 函数的顶部:

long lBreakAlloc = 0;
if ( lBreakAlloc > 0 )
{
    _CrtSetBreakAlloc( lBreakAlloc );
}

if然后在该语句上设置一个断点并启动您的程序。当它停在 时if,更改 的值lBreakAlloc以匹配您的第一个泄漏内存块的分配顺序号,然后让程序继续。

当它到达分配该内存块的位置时,嘿!它将在调试器中停止,您可以准确地看到哪个分配没有被释放。

于 2012-11-23T13:12:02.727 回答
2

unInitKinect课堂上的方法绝对是内存泄漏的来源。每个字段都设置为空,而不首先删除指向的对象(如果有的话)。我还发现了至少一种方法,它返回指向新对象的指针 ( selectKinect)。除非您仔细管理这些对象,否则它们也将成为内存泄漏的来源。

您可能希望考虑使用库来帮助您进行内存管理。Boost 的共享指针非常有用。

#include <boost/shared_ptr.hpp>

using boost::shared_ptr;

shared_ptr<Kinect> kinect(new Kinnect);

kinect->SomeMethodOnKinnect();

Boost 的共享指针使用引用计数来知道何时删除对象并在调用 shared_ptr 构造函数和析构函数时自动更新对象的引用计数。

你真的需要用细齿梳来检查你的代码,因为你的代码似乎充满了内存泄漏。下面的代码说它应该删除指针,但只是将它们设置为 NULL。

//Should delete a pointer and set it to NULL
void FaceTracking::Release()
{
    faceTrackingResult = NULL;
    nuiPresent = NULL;
    ColorBuffer = NULL;
    DepthBuffer = NULL;
}
于 2012-11-23T12:41:35.673 回答