找到谁通过智能指针创建对象 [英] finding who creates object via smart pointer

查看:215
本文介绍了找到谁通过智能指针创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了几天关于智能指针的内存泄漏。现在我可以找到哪些对象是泄漏的记忆,但我不能从它们泄漏的内存中找出它。



我使用相同的代码此处提及
检测引用计数对象中的内存泄漏



我已经读了很多评论,但没有一个解释正确。我尝试使用宏 FILE LINE ,它们都打印文件和refmanager类的行。



一个很好的方法来调试这个问题。请指出一些很好的例子。

解决方案

最简单的方法可能是在Windows上使用Valgrind和Linux上的一些类似工具。

您还可以在智能指针的构造函数中使用backtrace(在Linux和Mac上使用函数backtrace()函数)或者类似的函数从dbghelp.dll在Windows上,然后保存回溯在分配的某个地方,并删除backtrace在释放。然后在退出时打印所有剩余的追踪。


I posted few days back regarding memory leaks with smart pointers. Now I am able to find out which objects are leaking memory but I am not able to figure it out from where they are leaking memory.

I am using the same code as mentioned here Detecting memory leak in reference counted objects

I have read lot of comments but none of them explains properly. I tried using macros FILE and LINE , both of them prints file and line of refmanager class .

Is there a good way to debug this issue.Please point to some nice example also.

解决方案

The easiest way is probably to use Valgrind (on Linux and Mac) and some similar tool on Windows.

You could also in the constructor of the smart-pointer take a backtrace (using the function backtrace() function on Linux and Mac) or a similar function from dbghelp.dll on windows and then save the backtrace somewhere on allocation and remove the backtrace on deallocation. Then on exit print all the remaining backtraces.

这篇关于找到谁通过智能指针创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆