所以,这是我的考验。但它在最后一行有分段错误: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(...)
{
}
}