尝试在Gtest中清理对象时出现分段错误

时间:2017-05-04 09:42:48

标签: c++ unit-testing googletest

所以,这是我的考验。但它在最后一行有分段错误:m-> service->清洁。如果我不使用此功能,我会发生内存泄漏。还有其他选择吗?

class ServiceTest: public ::testing::Test
{
    protected:
    virtual void    SetUp()
    {
        m_service = new conn::tsm::Service();
    }

    virtual void    TearDown()
    {
        delete m_service;
        m_service = 0;
    }

    // Tested Object:
    conn::Service     *m_service;
};

TEST_F(ServiceTest, CatchChildDelete)
{
    conn::IObject_mock *testParam = new conn::IObject_mock();

    m_service->setId(0x01);
    EXPECT_CALL(*testParam, getId()).WillRepeatedly(::testing::Return(0x01));

    EXPECT_TRUE(m_service->addObject(testParam));

    EXPECT_CALL(*testParam, die()).WillOnce(::testing::Throw(std::exception()));

    m_service->clean();
}

这是clean()的实现:

void Service::clean() throw()
{
    try
    {
        std::vector<IObject *>::iterator it = m_objectTable.end();
        for( it = m_objectTable.begin(); it != m_objectTable.end();++it )
        {
            delete (*it);
        }

        /* clear cache */
        m_objectMap.clear();
        m_objectTable.clear();
    }
    catch(...)
    {
    }
}

0 个答案:

没有答案