检测到内存泄漏
在我的 wxWidgets 应用程序中,在调试模式下运行时,我在 Visual Studio 2010 的输出中收到此消息。应用程序运行良好,我只是在关闭它后才看到此消息。
检测到内存泄漏!
转储对象 ->
{9554} 正常块位于 0x003CDCC0,44 字节长。
数据:<结束> 20 C1 65 01 01 00 00 00 6E 00 00 00 9C CE 64 01{9553} 正常块位于 0x003CDB58,8 字节长。
数据:< D e < > 44 BD 65 01 C0 直流 3C 00
{9552} 正常块位于 0x003CDC50,48 字节长。数据:< e> A0 95 65 01 01 00 00 00 19 00 00 00 19 00 00 00
对象转储完成。
在我的程序中,我没有显式分配内存,但是 wxWidgets 框架是。第一次收到这样的消息,不知道具体原因。
我怎样才能摆脱这个内存泄漏?
In my wxWidgets application, while running in debug mode i got this message in Output of Visual Studio 2010. The application ran fine, and i only saw this after closing it.
Detected memory leaks!
Dumping objects ->
{9554} normal block at 0x003CDCC0, 44 bytes long.
Data: < e n d > 20 C1 65 01 01 00 00 00 6E 00 00 00 9C CE 64 01{9553} normal block at 0x003CDB58, 8 bytes long.
Data: < D e < > 44 BD 65 01 C0 DC 3C 00
{9552} normal block at 0x003CDC50, 48 bytes long.Data: < e > A0 95 65 01 01 00 00 00 19 00 00 00 19 00 00 00
Object dump complete.
In my program i am not explicitly allocating memory, however the wxWidgets framework is. I have got such a message for first time, and don't know the exact cause of it.
How can i get rid of this memory leak?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您只需在 main 函数的开头添加以下几行即可。添加此标志后,Visual Studio 将在造成内存泄漏的行处中断。
确保您拥有正确的对象正常块地址,因为它们可能会更改,并确保您在 _DEBUG 上进行编译。
另请参阅:_CrtSetDbgFlag 和 _CrtSetBreakAlloc。
You just have to add the following lines at the beginning of your main function. Adding this flag, Visual Studio will break at the line that is creating the memory leak.
Make sure you have the correct object normal block address because they might change and ensure you are compiling on _DEBUG.
See also: _CrtSetDbgFlag and _CrtSetBreakAlloc.
永远不要只是“假设”您的代码是防内存泄漏的。除非您是编程半神之一,否则没有人能免受可能的写入内存泄漏的影响。
您可以使用边界检查器(来自 Microfocus)之类的工具来帮助识别内存泄漏,因为它会给您一个调用堆栈。从调试 CRT 获得的内存泄漏报告仅告诉您在特定地址发生内存泄漏。像边界检查器这样的产品将为您提供内存泄漏的调用堆栈,以及许多其他好处。市场上还有其他内存泄漏工具,但我不会在这里列出它们。
如果您确定内存泄漏是由于“wxWidgets”造成的,那么也许您应该通知该库的编写者,也许他们会修复它(使用合适的重现步骤)。
Never just 'assume' that your code is memory leak proof. Unless you are one of the programming demi-gods, no one is immune from possibly writing memory leaks.
You could use a tool like bounds checker (From Microfocus) to help identify the memory leak because it will give you a callstack. The memory leak report you got from the debug CRT just tells you memory leaked at a particular address. A product like bounds checker will give you a callstack for that memory leak, along with lots of other goodies. There are other memory leak tools out there in the market, but I won't attempt to list them here.
If you are sure the memory leak is due to 'wxWidgets', then perhaps you should inform the writers of that library and perhaps they will fix it (With suitable repro steps).
也许某些类型的静态实例仍然由框架分配。尝试使用像“devpartner”这样的分析器来解决它。
Maybe some kinds of static instances are still allocated by the framework. Try to solve it with profiler like "devpartner".
此 wiki 建议在所有其他标头包含之后将以下内容添加到您拥有的每个源文件中:
这将导致程序结束时报告泄漏。
更具体地说,请确保对使用
new
创建的所有对象(顶部窗口除外)调用->Destroy()
。This wiki suggests adding the following to every source file you have, after all other header include's:
This will result in leaks being reported when your program ends.
More specifically, make sure you call
->Destroy()
on all objects you create usingnew
(except maybe your top window).如果 vs 报告的泄漏位置每次都相同,您可以设置一个数据断点来查看该内存何时被更改,并希望找出谁在分配该内存
If the location of the leak reported by vs is same every time you could set a databreakpoint to see when this memory is being changed and hopefully figure out who is allocating this memory