CppUnit泄漏

时间:2009-10-29 16:01:12

标签: c++ valgrind cppunit

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

==20341== 256 bytes in 1 blocks are indirectly lost in loss record 915 of 919                                                                                                         
==20341==    at 0x4A0661C: operator new(unsigned long) (vg_replace_malloc.c:220)                                                                                                      
==20341==    by 0x7F366FA: std::vector<CppUnit::Test*, std::allocator<CppUnit::Test*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<CppUnit::Test**, std::vector<CppUnit::Test*, std::allocator<CppUnit::Test*> > >, CppUnit::Test* const&) (new_allocator.h:88)                                                                                                            
==20341==    by 0x7F36496: CppUnit::TestSuite::addTest(CppUnit::Test*) (stl_vector.h:610)                                                                                             
==20341==    by 0x585B80: TestVectorAlgebra::addTestsToSuite(CppUnit::TestSuiteBuilderContextBase&) (testvectoralgebra.h:30)                                                          
==20341==    by 0x586719: TestVectorAlgebra::suite() (testvectoralgebra.h:42)                                                                                                         
==20341==    by 0x5948C4: CppUnit::TestSuiteFactory<TestVectorAlgebra>::makeTest() (TestSuiteFactory.h:20)                                                                            
==20341==    by 0x7F2C6B0: CppUnit::TestFactoryRegistry::addTestToSuite(CppUnit::TestSuite*) (TestFactoryRegistry.cpp:149)                                                            
==20341==    by 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") );

如何取消注册这些测试?

1 个答案:

答案 0 :(得分:2)

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

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

(如果你有时间和倾向,你可能想要研究一个不同的单元测试框架。UnitTest++Google Test比CppUnit更新,更容易使用,更有特色。)