boost会有内存泄漏?

liuhaoc 2006-12-12 11:48:43
我写了一个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 4E 4F 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中,一个函数包括以下调用:
bool CFoo::GetGo()
{
path cPath( "c:\\windows", native );
return m_bGo;
}

path 是boost-filesystem中的类。

主程序在BOOL CTestGUIApp::InitInstance()中用以下方式调用DLL:
CFoo* cf = CreateFoo();
bool bB = cf->GetGo();
DeleteFoo( cf );

一直无法想通为什么会出现内存泄漏,path 这个类中没有特别分配内存的啊。各位专家有谁有办法解决吗?
...全文
493 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhaoc 2006-12-14
  • 打赏
  • 举报
回复
我找到的可能原因: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.
whoho 2006-12-12
  • 打赏
  • 举报
回复
如果不存在这个问题,而且你程序中除此之外没有使用ACE,有可能是你别的地方有内存泄漏
积木 2006-12-12
  • 打赏
  • 举报
回复
看起来怪怪的,不敢乱说,不过boost是试验性质的,出个错误没啥奇怪的。
whoho 2006-12-12
  • 打赏
  • 举报
回复
vc环境下,如果你强制中断调试,就会出现内存泄漏的误报
roger_77 2006-12-12
  • 打赏
  • 举报
回复
boost::path会有这问题?

关注一下!
liuhaoc 2006-12-12
  • 打赏
  • 举报
回复
我找到了泄漏是由于以下常量定义引起的(path_posix_windows.cpp):
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._-" );

但是还是不知道如何避免,有人可以帮忙吗?
liuhaoc 2006-12-12
  • 打赏
  • 举报
回复
以上我写的程序都是用于验证这个问题的程序,很简单,没有实际用途。别的地方不会有内存泄漏,因为是最简单的MFC自动生成的窗口例子。
调试退出是正常方式退出,执行了所有的析构,不应该出现内存泄漏。
当我在MFC的BOOL CTestGUIApp::InitInstance()中直接用path类时,则不会报内存泄漏,放在DLL里用的时候就会报。很奇怪是吗?

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧