【问题标题】:CppUnit leakageCppUnit 泄漏
【发布时间】:2010-12-11 07:31:54
【问题描述】:

用 valgrind 运行我的回归测试我有这样的报告:

==20341== 1 个块中的 256 个字节在 919 的丢失记录 915 中间接丢失 ==20341== at 0x4A0661C: operator new(unsigned long) (vg_replace_malloc.c:220) ==20341== by 0x7F366FA: std::vector<:test std::allocator> >::_M_insert_aux(__gnu_cxx::__normal_iterator<:test std:: std::allocator> > >, CppUnit::Test* const&) (new_allocator.h:88) ==20341== by 0x7F36496: CppUnit::TestSuite::addTest(CppUnit::Test*) (stl_vector.h:610) ==20341== 0x585B80:TestVectorAlgebra::addTestsToSuite(CppUnit::TestSuiteBuilderContextBase&) (testvectoralgebra.h:30) ==20341== by 0x586719: TestVectorAlgebra::suite() (testvectoralgebra.h:42) ==20341== 0x5948C4:CppUnit::TestSuiteFactory::makeTest() (TestSuiteFactory.h:20) ==20341== 0x7F2C6B0:CppUnit::TestFactoryRegistry::addTestToSuite(CppUnit::TestSuite*) (TestFactoryRegistry.cpp:149) ==20341== 由 0x7F2CAD5: CppUnit::TestFactoryRegistry::makeTest() (TestFactoryRegistry.cpp:136) ==20341== by 0x580760: main (testunit.cpp:88)

我猜这是因为添加到 Suite 的测试在主程序结束之前没有被删除。

这是我注册测试的方式:

  CppUnit::TextTestRunner::TestRunner runner;

  // Get the top level suite from the registry
  CppUnit::Test* myTest = 
    CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();

  runner.addTest( myTest->findTest("TestVectorAlgebra") );

如何取消注册这些测试?

【问题讨论】:

  • 我也用一些 Windows 内存分析器诊断出这个泄漏。我发现 CppUnit 被过度设计了。换句话说,这是一堆垃圾。

标签: c++ valgrind cppunit


【解决方案1】:

CppUnit documentation 表明runner.addTest 拥有对其进行的任何测试的所有权。通过只提供您的 myTest 实例的一部分 runner.addTest,您并没有为整个 myTest 实例在删除时得到清理提供任何方法。运行后手动delete'ing myTest 可能也不起作用,因为runner 也会尝试删除myTest 中已给出的部分。

如果您只对运行特定测试或测试子集感兴趣,则应尝试使用TextRunner::runtestName 参数。

(如果您有时间和兴趣,您可能想研究一个不同的单元测试框架。UnitTest++Google Test 比 CppUnit 更新、更易于使用且功能更多。)

【讨论】:

  • 如果我没有为要运行的测试注册套件,TextRunner::run 将不起作用。
  • 是的,但是您的问题是您分配了整个套件(myTest),然后只注册了其中的一部分。 TextRunner 会清理该部分,但不知道清理整个套件。
  • 即使我做 runner.addTest(myTest);然后我有同样的行为,很多套房没有被破坏。
  • 我不知道还能告诉你什么;对不起。当我通过 Valgrind 运行一个非常简单的 CppUnit 程序时,我得到了正确的行为;也许您正在使用的 CppUnit 版本存在错误,或者泄漏可能在您尚未发布的代码中。自从我使用 CppUnit 已经有一段时间了;我们将所有内容都切换到了 Google 测试。
  • 我快速浏览了 Google Test,迁移我们的测试应该不难,最难的是我们使用自己的 TextOutputter(扩展 CppUnit::TextOutputter)和我们自己的 TestListener (扩展 CppUnit::TestListener )。谢谢你的提示。
猜你喜欢
  • 2010-11-03
  • 2011-11-02
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
相关资源
最近更新 更多