使用CppUnit进行内存泄漏检测

时间:2023-01-13 10:26:23

Is anyone aware of extensions to CppUnit that can be used to make assertions on a test by test basis concerning memory leaks.

是否有人知道CppUnit的扩展可用于通过测试基础对内存泄漏进行测试断言。

i.e. CPPUNIT_ASSERT_NO_LEAKS()?

Essentially, I want to be able to fail specific tests when the execution of the test results in leaked memory.

本质上,我希望能够在测试执行导致泄漏内存时失败特定测试。

8 个解决方案

#1


If you're running on Linux, you could run your tests with memcheck.

如果您在Linux上运行,则可以使用memcheck运行测试。

The Client Requests section of the manual describes several useful macros, of which one is noted as being useful for testing:

本手册的“客户端请求”部分描述了几个有用的宏,其中一个被认为对测试有用:

VALGRIND_COUNT_LEAKS: fills in the four arguments with the number of bytes of memory found by the previous leak check to be leaked, dubious, reachable and suppressed. Again, useful in test harness code, after calling VALGRIND_DO_LEAK_CHECK.

VALGRIND_COUNT_LEAKS:填充四个参数,前面的泄漏检查找到的内存字节数被泄露,可疑,可达和抑制。再次,在调用VALGRIND_DO_LEAK_CHECK之后,在测试工具代码中很有用。

The macro is defined in memcheck.h (likely in /usr/include/valgrind), and the sequence you want will resemble

宏在memcheck.h中定义(可能在/ usr / include / valgrind中),你想要的序列将类似

unsigned long base_definite, base_dubious, base_reachable, base_suppressed;
VALGRIND_DO_LEAK_CHECK;
VALGRIND_COUNT_LEAKS(base_definite, base_dubious, base_reachable, base_suppressed);
// maybe assert that they're zero!

// call test

unsigned long leaked, dubious, reachable, suppressed;
VALGRIND_DO_LEAK_CHECK;
VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed);
CPPUNIT_ASSERT_EQUAL(base_leaked, leaked);
// etc.

Repeating that for every test would be a pain, so you might write macros of your own or, even better, a specialized TestRunner.

重复每次测试都会很痛苦,所以你可能会编写自己的宏,或者更好的是专门的TestRunner。

#2


CPPUNIT doesn't have memory leak checks support by default.

默认情况下,CPPUNIT没有内存泄漏检查支持。

The project has been recontinued now (it was stopped for a long time) and this may be a feature of CPPUNIT2, you can propose (or write) to the authors.

该项目现已重新审核(已停止很长时间),这可能是CPPUNIT2的一个功能,您可以向作者提议(或写作)。

If you're looking for a Unit test framework with memory leak detection support you can try looking at CppUTest. It is the project used by Martin Fowler and Bob Martin on some TDD courses. It is pretty good.

如果您正在寻找具有内存泄漏检测支持的单元测试框架,您可以尝试查看CppUTest。这是Martin Fowler和Bob Martin在一些TDD课程中使用的项目。非常好。

#3


On Windows it would be a pretty simple matter of using some calls to the debug heap to get CppUnit to act on this information using _CrtMemCheckpoint() and _CrtMemDifference():

在Windows上,使用对调试堆的一些调用来使CppUnit使用_CrtMemCheckpoint()和_CrtMemDifference()对此信息进行操作将是一个非常简单的问题:

There are drawbacks:

有缺点:

  • you'd have to place something manually at the start of the test to get the checkpoint (maybe there's a way to integrate that into CppUnit somehow)
  • 你必须在测试开始时手动放置一些东西以获得检查点(也许有办法将它集成到CppUnit中)

  • it's Windows only (there's probably something similar on the various other platforms)
  • 它只是Windows(在其他各种平台上可能有类似的东西)

  • it'll only work for builds with the Debug CRT
  • 它只适用于使用Debug CRT的构建

#4


Where I work we build our unit tests with purify. Then our continuous integration platform pulls both the number of testcases that succeeded/failed and the number of leaked bytes (+ lint and coverity results) and shows it on a web page. I can highly recommend doing it this way.

在我工作的地方,我们用purify构建我们的单元测试。然后,我们的持续集成平台同时提取成功/失败的测试用例数量和泄漏字节数(+ lint和覆盖率结果),并在网页上显示。我强烈建议这样做。

Sorry for not providing the solution you wanted.

很抱歉没有提供您想要的解决方案。

#5


I know it's a little too late to answer to this question. But here is a great tool from Microsoft. I am a linux user now but I have used this when I was writing codes in windows (Visual C++ and Qt) http://www.microsoft.com/en-us/download/details.aspx?id=20028

我知道回答这个问题有点太晚了。但这是微软的一个很棒的工具。我现在是一个linux用户,但是当我在windows中编写代码时使用了这个(Visual C ++和Qt)http://www.microsoft.com/en-us/download/details.aspx?id=20028

#6


No idea of that, but you could use something like the Fluid Studios Memory Manager code and hook that in yourself with some tweaking. Either that or compile it into your test application and then have a script that runs the application once for each test and collate the memory tracking results.

不知道这一点,但你可以使用像Fluid Studios内存管理器代码这样的东西,并通过一些调整来解决这个问题。要么将其编译到测试应用程序中,然后为每个测试运行一次应用程序,并整理内存跟踪结果。

#7


Run your unit tests with valgrind. The unit test framework which I use allows you to run one or more individual unit tests so you can detect which one is causing the leak.

使用valgrind运行单元测试。我使用的单元测试框架允许您运行一个或多个单独的单元测试,以便您可以检测哪一个导致泄漏。

#8


I know it's not CppUnit, but boost::test can do memory leak detection.

我知道它不是CppUnit,但boost :: test可以进行内存泄漏检测。

From http://www.boost.org/doc/libs/1_39_0/libs/test/doc/html/execution-monitor/user-guide.html:

void detect_memory_leaks( bool on_off );

void detect_memory_leaks(bool on_off);

void break_memory_alloc( long mem_alloc_order_num );

void break_memory_alloc(long mem_alloc_order_num);

#1


If you're running on Linux, you could run your tests with memcheck.

如果您在Linux上运行,则可以使用memcheck运行测试。

The Client Requests section of the manual describes several useful macros, of which one is noted as being useful for testing:

本手册的“客户端请求”部分描述了几个有用的宏,其中一个被认为对测试有用:

VALGRIND_COUNT_LEAKS: fills in the four arguments with the number of bytes of memory found by the previous leak check to be leaked, dubious, reachable and suppressed. Again, useful in test harness code, after calling VALGRIND_DO_LEAK_CHECK.

VALGRIND_COUNT_LEAKS:填充四个参数,前面的泄漏检查找到的内存字节数被泄露,可疑,可达和抑制。再次,在调用VALGRIND_DO_LEAK_CHECK之后,在测试工具代码中很有用。

The macro is defined in memcheck.h (likely in /usr/include/valgrind), and the sequence you want will resemble

宏在memcheck.h中定义(可能在/ usr / include / valgrind中),你想要的序列将类似

unsigned long base_definite, base_dubious, base_reachable, base_suppressed;
VALGRIND_DO_LEAK_CHECK;
VALGRIND_COUNT_LEAKS(base_definite, base_dubious, base_reachable, base_suppressed);
// maybe assert that they're zero!

// call test

unsigned long leaked, dubious, reachable, suppressed;
VALGRIND_DO_LEAK_CHECK;
VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed);
CPPUNIT_ASSERT_EQUAL(base_leaked, leaked);
// etc.

Repeating that for every test would be a pain, so you might write macros of your own or, even better, a specialized TestRunner.

重复每次测试都会很痛苦,所以你可能会编写自己的宏,或者更好的是专门的TestRunner。

#2


CPPUNIT doesn't have memory leak checks support by default.

默认情况下,CPPUNIT没有内存泄漏检查支持。

The project has been recontinued now (it was stopped for a long time) and this may be a feature of CPPUNIT2, you can propose (or write) to the authors.

该项目现已重新审核(已停止很长时间),这可能是CPPUNIT2的一个功能,您可以向作者提议(或写作)。

If you're looking for a Unit test framework with memory leak detection support you can try looking at CppUTest. It is the project used by Martin Fowler and Bob Martin on some TDD courses. It is pretty good.

如果您正在寻找具有内存泄漏检测支持的单元测试框架,您可以尝试查看CppUTest。这是Martin Fowler和Bob Martin在一些TDD课程中使用的项目。非常好。

#3


On Windows it would be a pretty simple matter of using some calls to the debug heap to get CppUnit to act on this information using _CrtMemCheckpoint() and _CrtMemDifference():

在Windows上,使用对调试堆的一些调用来使CppUnit使用_CrtMemCheckpoint()和_CrtMemDifference()对此信息进行操作将是一个非常简单的问题:

There are drawbacks:

有缺点:

  • you'd have to place something manually at the start of the test to get the checkpoint (maybe there's a way to integrate that into CppUnit somehow)
  • 你必须在测试开始时手动放置一些东西以获得检查点(也许有办法将它集成到CppUnit中)

  • it's Windows only (there's probably something similar on the various other platforms)
  • 它只是Windows(在其他各种平台上可能有类似的东西)

  • it'll only work for builds with the Debug CRT
  • 它只适用于使用Debug CRT的构建

#4


Where I work we build our unit tests with purify. Then our continuous integration platform pulls both the number of testcases that succeeded/failed and the number of leaked bytes (+ lint and coverity results) and shows it on a web page. I can highly recommend doing it this way.

在我工作的地方,我们用purify构建我们的单元测试。然后,我们的持续集成平台同时提取成功/失败的测试用例数量和泄漏字节数(+ lint和覆盖率结果),并在网页上显示。我强烈建议这样做。

Sorry for not providing the solution you wanted.

很抱歉没有提供您想要的解决方案。

#5


I know it's a little too late to answer to this question. But here is a great tool from Microsoft. I am a linux user now but I have used this when I was writing codes in windows (Visual C++ and Qt) http://www.microsoft.com/en-us/download/details.aspx?id=20028

我知道回答这个问题有点太晚了。但这是微软的一个很棒的工具。我现在是一个linux用户,但是当我在windows中编写代码时使用了这个(Visual C ++和Qt)http://www.microsoft.com/en-us/download/details.aspx?id=20028

#6


No idea of that, but you could use something like the Fluid Studios Memory Manager code and hook that in yourself with some tweaking. Either that or compile it into your test application and then have a script that runs the application once for each test and collate the memory tracking results.

不知道这一点,但你可以使用像Fluid Studios内存管理器代码这样的东西,并通过一些调整来解决这个问题。要么将其编译到测试应用程序中,然后为每个测试运行一次应用程序,并整理内存跟踪结果。

#7


Run your unit tests with valgrind. The unit test framework which I use allows you to run one or more individual unit tests so you can detect which one is causing the leak.

使用valgrind运行单元测试。我使用的单元测试框架允许您运行一个或多个单独的单元测试,以便您可以检测哪一个导致泄漏。

#8


I know it's not CppUnit, but boost::test can do memory leak detection.

我知道它不是CppUnit,但boost :: test可以进行内存泄漏检测。

From http://www.boost.org/doc/libs/1_39_0/libs/test/doc/html/execution-monitor/user-guide.html:

void detect_memory_leaks( bool on_off );

void detect_memory_leaks(bool on_off);

void break_memory_alloc( long mem_alloc_order_num );

void break_memory_alloc(long mem_alloc_order_num);