2009-10-29 69 views
3

運行我的迴歸測試的valgrind與我有這樣的報告:CppUnit的泄漏

 
==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) 

我想這是因爲沒有去除的測試,加入到套件之前其實主要就是結束了。

這是我註冊的測試方式:

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")); 

如何註銷這些測試?

+0

我診斷此泄漏以及一些Windows內存分析器以及。我發現CppUnit是過度設計的方式。換句話說,這是一堆垃圾。 – 2011-05-10 17:02:50

回答

2

CppUnit documentation建議runner.addTest接受任何測試的所有權。通過給runner.addTest僅提供myTest實例的一部分,您並未提供任何方式讓整個myTest實例在刪除時得到清理。手動運行delete在運行後運行myTest可能也不起作用,因爲runner也會嘗試刪除已給出的部分myTest

如果您只想運行特定測試或測試子集感興趣,則應該嘗試使用TextRunner::run的參數testName

(如果你有時間和興趣,你可能想尋找到一個不同的單元測試框架。UnitTest++Google Test是較新的,更容易使用,而且比CppUnit的更多的其他功能。)

+0

如果我沒有爲我想運行的測試註冊套件,TextRunner :: run不起作用。 – 2009-10-30 08:51:35

+0

是的,但你的問題是你分配一整套(myTest),然後只註冊一部分。 TextRunner清理那部分,但不知道清理整個套件。 – 2009-10-30 12:00:55

+0

即使我做了runner.addTest(myTest);那麼我也有同樣的行爲,很多套房沒有被毀滅。 – 2009-10-30 20:51:15