写个ACE的最简单程序,居然有内存泄漏!
程序代码:
// //检测内存泄漏 >>> #define _CRTDBG_MAP_ALLOC #include <crtdbg.h> #ifdef _DEBUG #define new new(_NORMAL_BLOCK,__FILE__,__LINE__) #endif //检测内存泄漏 <<< // #include<stdio.h> #include "ace/ACE.h" int main (int argc, char *argv[]) { getchar(); _CrtDumpMemoryLeaks(); // 检测内存泄漏 return 0; }
如果不修改ACE的源代码,编译执行后提示有一堆内存泄漏:
Detected memory leaks!
Dumping objects ->
{161} normal block at 0x003FD448, 8 bytes long.
Data: < ? > A0 BB 3F 00 CD CD CD CD
{160} normal block at 0x003FD3F8, 20 bytes long.
Data: <\ a > 5C A1 1A 10 0A 01 00 00 61 18 00 10 03 00 00 00
{159} normal block at 0x003FD398, 32 bytes long.
Data: < P > EC A2 1A 10 88 50 15 00 FF FF FF FF 00 00 00 00
{158} normal block at 0x003FD338, 32 bytes long.
Data: < PP > EC A2 1A 10 50 50 15 00 FF FF FF FF 00 00 00 00
{157} normal block at 0x003FD2D8, 32 bytes long.
Data: < P > DC A2 1A 10 18 50 15 00 FF FF FF FF 00 00 00 00
{156} normal block at 0x003FD290, 8 bytes long.
Data: < > FC A2 1A 10 00 00 00 00
{155} normal block at 0x003FD230, 32 bytes long.
Data: < O > DC A2 1A 10 E0 4F 15 00 FF FF FF FF 00 00 00 00
{154} normal block at 0x003FD1D0, 32 bytes long.
Data: < O > EC A2 1A 10 A8 4F 15 00 FF FF FF FF 00 00 00 00
{153} normal block at 0x003FD170, 32 bytes long.
Data: < pO > EC A2 1A 10 70 4F 15 00 FF FF FF FF 00 00 00 00
{152} normal block at 0x003FD110, 32 bytes long.
Data: < 8O > DC A2 1A 10 38 4F 15 00 FF FF FF FF 00 00 00 00
{151} normal block at 0x003FD028, 172 bytes long.
Data: < XN > CC A2 1A 10 01 00 00 00 58 4E 15 00 FF FF FF FF
{150} normal block at 0x003FCFB8, 52 bytes long.
Data: <T > 54 A2 1A 10 01 00 00 00 00 00 00 00 00 00 00 00
{149} normal block at 0x003FCF78, 4 bytes long.
Data: < > FF FF FF FF
{148} normal block at 0x003FCF20, 24 bytes long.
Data: < J > 18 4A 15 00 FF FF FF FF 00 00 00 00 00 00 00 00
{147} normal block at 0x003FCEC8, 24 bytes long.
Data: < I > E0 49 15 00 FF FF FF FF 00 00 00 00 00 00 00 00
{146} normal block at 0x003FCE70, 24 bytes long.
Data: < I > A8 49 15 00 FF FF FF FF 00 00 00 00 00 00 00 00
{145} normal block at 0x003FCE08, 40 bytes long.
Data: <0 H ? > 30 A4 1A 10 02 00 00 00 01 CD CD CD 48 CD 3F 00
{144} normal block at 0x003FCDB0, 28 bytes long.
Data: <pI > 70 49 15 00 FF FF FF FF 00 00 00 00 00 00 00 00
{143} normal block at 0x003FCD48, 44 bytes long.
Data: < > B8 A2 1A 10 02 00 00 00 01 CD CD CD 00 00 00 00
{140} normal block at 0x003FBC30, 28 bytes long.
Data: <0 ? $ > 30 BC 3F 00 D4 AA 24 10 00 00 00 00 00 00 00 00
{139} normal block at 0x003FBBE8, 8 bytes long.
Data: <H ? ? > 48 D4 3F 00 F8 D3 3F 00
{138} normal block at 0x003FBBA0, 8 bytes long.
Data: < ? $ > E8 BB 3F 00 B0 85 24 10
{137} normal block at 0x003FBB50, 16 bytes long.
Data: <H ? $ > 48 D4 3F 00 02 00 00 00 D4 AA 24 10 CD CD CD CD
{136} normal block at 0x003FBA88, 136 bytes long.
Data: < > 80 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00
{129} normal block at 0x003FBA30, 24 bytes long.
Data: <0 ? $ > 30 BA 3F 00 D4 AA 24 10 00 00 00 00 00 00 00 00
{128} normal block at 0x003FB9E8, 12 bytes long.
Data: <0 ? $ > 30 BA 3F 00 00 00 00 00 D4 AA 24 10
{127} normal block at 0x003F99A8, 8192 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
{126} normal block at 0x003F9940, 40 bytes long.
Data: < ? 8I > 00 00 00 00 00 04 00 00 A8 99 3F 00 38 49 15 00
{125} normal block at 0x003F98E0, 32 bytes long.
Data: < I > DC A2 1A 10 00 49 15 00 FF FF FF FF 00 00 00 00
{124} normal block at 0x003F9878, 44 bytes long.
Data: < t > 00 CD CD CD 00 04 00 00 00 00 00 00 74 FD 1A 10
{123} normal block at 0x003F6538, 13060 bytes long.
Data: < > 00 00 00 00 00 00 00 00 FF FF FF FF 00 00 00 00
{122} normal block at 0x003F64D0, 44 bytes long.
Data: < H > 00 00 00 00 90 48 15 00 FF FF FF FF 00 00 00 00
{121} normal block at 0x003F6478, 28 bytes long.
Data: <XH > 58 48 15 00 FF FF FF FF 00 00 00 00 00 00 00 00
{120} normal block at 0x003F6408, 52 bytes long.
Data: < x ? > F0 FA 1A 10 D0 FA 1A 10 E4 FA 1A 10 78 98 3F 00
Object dump complete.
怎么排除这些内存泄漏? 这样做:
修改 ACE 的 OS_main.h 文件。
OS_main.h文件里部分内容:
# define main \ ace_main_i (int, char *[]); \ ACE_BEGIN_VERSIONED_NAMESPACE_DECL \ ACE_Export int ace_os_main_i (ACE_Main_Base&, int, char *[]); \ class ACE_Main : public ACE_Main_Base {int run_i (int, char *[]);}; \ inline int ACE_Main::run_i (int argc, char *argv[]) \ { \ return ace_main_i (argc, argv); \ } \ ACE_END_VERSIONED_NAMESPACE_DECL \ int \ ACE_MAIN (int argc, char *argv[]) /* user's entry point, e.g., wmain */ \ { \ ACE_Main m; \ return m.run (argc, argv); /*ace_os_main_i (m, argc, argv); what the user calls "main" */ \ } \ int \ ace_main_i
只修改这两句:
ACE_Main m; \ // 删掉这句。
return m.run (argc, argv); /*ace_os_main_i (m, argc, argv); what the user calls "main" */ \ // 把 return 后面换成 ace_main_i (argc, argv); \
修改后如下:
# define main \ ace_main_i (int, char *[]); \ ACE_BEGIN_VERSIONED_NAMESPACE_DECL \ ACE_Export int ace_os_main_i (ACE_Main_Base&, int, char *[]); \ class ACE_Main : public ACE_Main_Base {int run_i (int, char *[]);}; \ inline int ACE_Main::run_i (int argc, char *argv[]) \ { \ return ace_main_i (argc, argv); \ } \ ACE_END_VERSIONED_NAMESPACE_DECL \ int \ ACE_MAIN (int argc, char *argv[]) /* user's entry point, e.g., wmain */ \ { \ return ace_main_i (argc, argv); \ } \ int \ ace_main_i
编译 ACE 重新生成 ACE 的 dll 和 lib 库后,再执行上面的小程序,内存泄漏没有了。
-----------------------------------------------------------------------
上述方法改变了ACE代码结构,当然这是不应该滴,经考证实际上也没那么多内存泄漏。
正确做法如下(原因参考我的其他关于内存泄漏的博文)。
小程序代码更简单,不需要填写检测内存泄漏的代码:
#include<stdio.h> #include "ace/ACE.h" int main (int argc, char *argv[]) { getchar(); return 0; }
下面把内存泄漏检测代码加到 ACE 的 OS_main.cpp 文件里。
OS_main.cpp 原始部分内容:
ACE_Main_Base::~ACE_Main_Base () { ACE::fini (); }
修改这部分代码,把内存泄漏检测代码加入其中:
#define _CRTDBG_MAP_ALLOC #include <crtdbg.h> ACE_Main_Base::~ACE_Main_Base () { ACE::fini (); _CrtDumpMemoryLeaks(); // 检测内存泄漏 }
重新编译ACE,编译运行小程序后,还提示有一个内存泄漏:
Detected memory leaks!
Dumping objects ->
{140} normal block at 0x003FBC20, 28 bytes long.
Data: < ? $ > 20 BC 3F 00 D4 AA 24 10 00 00 00 00 00 00 00 00
Object dump complete.
就剩一个内存泄漏了,比以前好看多了。
这个内存泄漏还没找到是什么原因,是不是真的内存泄漏还不知道。如果你知道,就给我留言告诉我哦~