读书人

坑爹的VS2008内存储器泄露报告

发布时间: 2012-09-05 15:19:35 作者: rapoo

坑爹的VS2008内存泄露报告

作者:朱金灿

来源:http://blog.csdn.net/clever101

最近在设计和开发一个软件框架,这个框架包含一个exe工程和多个dll工程。基本框架差不多了,等到调试运行时,出现一个莫名其妙的内存泄露,程序退出时提示:

Dumping objects ->
{119} normal block at 0x015A63A8, 80 bytes long.
Data: <ABCDEFGHIJKLMNOP> 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E4F 50
{118} normal block at 0x015A6338, 48 bytes long.
Data: < > 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
Object dump complete.

因为是新建的框架,我是比较注意解决内存泄露问题,内存泄露这玩意,在开始时不注意防范,到后面就会越积越多的。开始还不知道哪个模块出现了内存泄露,因为根本没有提示在哪个源代码文件出现内存泄露。我使用《如何调试MFC中的内存泄漏》来查找内存泄露的模块,没找着。然后在每个模块的初始化函数加上:



程序在退出时触发了,通过查看堆栈发现是boost_filesystem-vc90-mt-gd-1_42.dll造成的。然后想解决这个问题。到
网上搜索资料,终于搜到了有人和我遇到一样的问题的一篇帖子:boost会有内存泄漏?

该帖子说:我写了一个MFC程序调用我自己写的win32 DLL (非MFC DLL),在DLL中调用了boost的filesystem库,在主程序退出调试后,output窗口中报告出现了内存泄漏:

Detected memory leaks!
Dumping objects ->
{119} normal block at 0x00396060, 80 bytes long.
Data: <ABCDEFGHIJKLMNOP> 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E4F 50
{118} normal block at 0x00395FF0, 48 bytes long.
Data: < > 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
Object dump complete.

在我的DLL中,一个函数包括以下调用:

    const   char   invalid_chars[]   =        "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F "        "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F "        " <> :\ "/\\| ";    //   note   that   the   terminating   '\0 '   is   part   of   the   string   -   thus   the   size   below    //   is   sizeof(invalid_chars)   rather   than   sizeof(invalid_chars)-1.     I      const   std::string   windows_invalid_chars(   invalid_chars,   sizeof(invalid_chars)   );    const   std::string   valid_posix(        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._- "   );

找到的可能原因:MFC的内存泄漏报告有bug,他在DLL中static 变量析构前报了。

参考:

MFC dumps leaks prematurely when it exits, instead of waiting for the
CRT to dump leaks following static data destruction, and this causes
spurious leak reports for objects which allocated memory before MFC
was initialized and which thus are destroyed after MFC exits. This is
commonly observed when using the MFC DLL in a program that also uses
the C++ runtime DLL.

打算以后建个工程测试下,如果确认有这个问题,就提交给微软和boost.org。




读书人网 >VSTS

热点推荐