运行单元测试时的内存泄漏检测 [英] Memory leak detection while running unit tests

查看:102
本文介绍了运行单元测试时的内存泄漏检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一组单元测试的Win32 C ++应用程序.单元测试运行完毕后,我希望可以自动生成任何可读的关于未释放内存的报告.理想情况下,报告将包含文件&每个未释放分配的行号信息.最好以一致的顺序生成它们,以便轻松地将其从一次运行与下一次运行进行区分. (基本上,我希望valgrind --leak-check = full的结果,但在Windows上).

I've got a Win32 C++ app with a suite of unit tests. After the unit tests have finished running, I'd like a human-readable report on any unfreed memory to be automatically generated. Ideally, the report will have a stack with files & line number info for each unfreed allocation. It would be nice to have them generated in a consistent order to make it easy to diff it from one run to the next. (Basically, I would like the results of valgrind --leak-check=full, but on windows).

我在UMDH方面取得了成功,它从正在运行的进程中获取了此类信息,但是只有当您附加到现有进程时,该工具才能起作用.我希望每次运行单元测试时都会自动发生这种情况.

I've had success with UMDH getting this kind of info from running processes, but that tool only seems to work if you attach to an existing process. I want this to happen automatically every time I run my unit tests.

有没有可以做到这一点的工具?如果可以,该如何使用?

Is there a tool that can do this? If so, how do I use it?

谢谢!

推荐答案

我使用了CRT调试堆函数Mike B指出,但最终我不满足于仅获得泄漏内存的地址.获得像UMDH这样的堆栈可以使调试更快.所以,现在在我的main()函数中 在运行测试以获取堆快照之前和之后,使用CreateProcess启动UMDH.我还编写了一个简单的批处理文件,该文件运行我的测试工具,然后比较堆快照.因此,我启动了批处理文件,并一次获得了测试结果和一个包含所有未释放分配的全部堆栈的文本文件.

I played around with the CRT Debug Heap functions Mike B pointed out, but ultimately I wasn't satisfied just getting the address of the leaked memory. Getting the stacks like UMDH provides makes debugging so much faster. So, in my main() function now I launch UMDH using CreateProcess before and after I run the tests to take heap snapshots. I also wrote a trivial batch file that runs my test harness and then diffs the heap snapshots. So, I launch the batch file and get my test results and a text file with the full stacks of any unfreed allocations all in one shot.

UMDH会收到很多误报,因此也许将CrtDebug的某些内容与我现在正在做的事情混合使用会是一个更好的解决方案.但是就目前而言,我对自己所拥有的感到满意.

UMDH picks up a lot of false positives, so perhaps some hybrid of the CrtDebug stuff and what I'm doing now would be a better solution. But for right now I'm happy with what I've got.

现在,如果我只是有一种方法可以检测到我是否没有关闭任何句柄……

Now if I just had a way to detect if I was not closing any handles...

这篇关于运行单元测试时的内存泄漏检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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