gpt4 book ai didi

c++ - CppUnit泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:13:53 26 4
gpt4 key购买 nike

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

I guess this is due the fact that Tests added to Suite are not removed before the main is over.

This is the way I register the test:

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

如何注销这些测试?

最佳答案

CppUnit documentation建议 runner.addTest 获取它所提供的任何测试的所有权。通过仅提供 myTest 实例的一部分 runner.addTest,您没有提供任何方式让整个 myTest 实例在删除时得到清理。运行后手动delete'ing myTest 可能也不会起作用,因为runner 也会尝试删除 myTest 中给它的部分.

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

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

关于c++ - CppUnit泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1644766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com