在清理时cppunit中的分段错误

时间:2013-01-09 16:55:37

标签: c++ cppunit

CPPUNIT在执行程序后崩溃,同时在清理阶段从main返回。 TestWrapping的dtor调用TestSuite的dtor,然后调用deleteContents来触发测试用例的清理。

有什么奇怪的是,TestSuite的dtor会被调用两次? 这是在成功执行6个测试用例之后。关于如何避免这种情况的任何想法?

    Program terminated with signal 11, Segmentation fault.
#0  0x0000000000000045 in ?? ()
(gdb) bt
#0  0x0000000000000045 in ?? ()
#1  0x0000000000000001 in ?? ()
#2  0x0000000001004f2d in CppUnit::TestSuite::~TestSuite (this=0x7fe7bc005820, __in_chrg=<optimized out>) at TestSuite.cpp:18
#3  0x0000000001004ebd in CppUnit::TestSuite::deleteContents (this=0x7fe7bc001040) at TestSuite.cpp:28
#4  0x000000000100500d in CppUnit::TestSuite::~TestSuite (this=0x7fe7bc005820, __in_chrg=<optimized out>) at TestSuite.cpp:18
#5  0x0000000001004c50 in CppUnit::TestRunner::WrappingSuite::~WrappingSuite (this=0x7fe7bc005820, __in_chrg=<optimized out>) at ../../include/cppunit/TestRunner.h:101
#6  0x000000000040b72a in main (argc=0, argv=0x7fff8198bf08) at /project/EAB3_EMC/BRF/lmcgupe/brf/build/../software/brfc_test/BrfcTestMain.cc:447


Code exercising this:
(from main)
    CppUnit::TextUi::TestRunner runner;
    CPPUNIT_NS::TestResult controller;
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener( &result );

    // Show a message as each test starts
    //
    CppUnit::BriefTestProgressListener listener;
    runner.eventManager().addListener(&listener);
    controller.addListener( &listener );

    // Specify XML output and inform the runner of this format
    //
    std::ofstream xmlout("test_result.xml");
    CppUnit::XmlOutputter* outputter = new CppUnit::XmlOutputter(
            &result, xmlout);
    runner.setOutputter(outputter);


    CppUnit::TextOutputter consoleOutputter(&result , std::cout);

    runner.addTest(CreateAlarmBackupSuite::suite());
    runner.run( controller );

from class CreateAlarmBackupSuite: public CppUnit::TestFixture

    static CppUnit::Test *suite()
    {

        // Create the Test Suite
        //
        CppUnit::TestSuite *suite = new CppUnit::TestSuite("CreateAlarmBackupSuite");

        // Add the test cases

        //Crt_Syst_07
        suite->addTest(new CppUnit::TestCaller<CreateAlarmBackupSuite>(
                       "07_Crt_ScheduledBackup_ScheduledSingleEvent_SystemDataBackup_Non_Successful_Create_Persistent_ManualDelNotClear",
                &CreateAlarmBackupSuite::Crt_ScheduledBackup_ScheduledSingleEvent_SystemDataBackup_Non_Successful_Create_Persistent_ManualDelNotClear));



        //Crt_Syst_09
        suite->addTest(new CppUnit::TestCaller<CreateAlarmBackupSuite>(
                "09_Crt_ScheduledBackup_ScheduledSingleEvent_SystemDataBackup_Non_Successful_Create_Transient_NoRetry",
                &CreateAlarmBackupSuite::Crt_ScheduledBackup_ScheduledSingleEvent_SystemDataBackup_Non_Successful_Create_Transient_NoRetry));

return suite;
}

1 个答案:

答案 0 :(得分:0)

我希望你的代码不是显式地调用这些对象的析构函数,而是在指向它们的指针上调用delete,否则它们会超出范围而编译器会自动调用它们的析构函数。假设你没有明确地调用析构函数.....

看起来您的TestSuite对象要么执行delete this,要么它包含指向自身的指针(或者在deleteContents()中以某种方式获取指向自身的指针)然后在该指针上调用delete 。您尚未发布TestSuite类的来源(特别是其deleteContents()方法),但这是我的猜测,没看过代码。