Collection CollectionFactory::createFromMap(const std::string& name,
const DataMap& dm) const
{
if (!Collection::isNameValid(name))
{
const std::string error = "invalid collection name";
throw std::invalid_argument(error);
}
Collection c(name, dm);
dm.initDataCollection(&c, true);
return c;
}
每当执行throw语句时,我都会收到一个分段错误。下面是Valgrind输出的原因。我不知道发生了什么。
==21124== Invalid read of size 1
==21124== at 0x41D2190: parse_lsda_header(_Unwind_Context*, unsigned char const*, lsda_header_info*) (eh_personality.cc:62)
==21124== by 0x41D24A9: __gxx_personality_v0 (eh_personality.cc:228)
==21124== by 0x4200220: _Unwind_RaiseException (unwind.inc:109)
==21124== by 0x41D2C9C: __cxa_throw (eh_throw.cc:75)
==21124== by 0x4079BFB: corestore::CollectionFactory::createFromMap(std::string const&, corestore::DataMap const&) const (CollectionFactory.C:43)
==21124== by 0x8188F86: CollectionFactoryTest::testCreateNewFromMap_InvalidName() (CollectionFactoryTest.C:91)
==21124== by 0x81895D3: CppUnit::TestCaller<CollectionFactoryTest>::runTest() (TestCaller.h:166)
==21124== by 0x40D1BB5: CppUnit::TestCaseMethodFunctor::operator()() const (TestCase.cpp:34)
==21124== by 0x40C18E3: CppUnit::DefaultProtector::protect(CppUnit::Functor const&, CppUnit::ProtectorContext const&) (DefaultProtector.cpp:15)
==21124== by 0x40CD0FC: CppUnit::ProtectorChain::ProtectFunctor::operator()() const (ProtectorChain.cpp:20)
==21124== by 0x40CCA65: CppUnit::ProtectorChain::protect(CppUnit::Functor const&, CppUnit::ProtectorContext const&) (ProtectorChain.cpp:77)
==21124== by 0x40DC6C4: CppUnit::TestResult::protect(CppUnit::Functor const&, CppUnit::Test*, std::string const&) (TestResult.cpp:178)
==21124== Address 0xc82f is not stack'd, malloc'd or (recently) free'd
我已经有过几次单元测试的迭代,但这里是当前的一个,它显示了与所有其他单元测试相同的bug:
void CollectionFactoryTest::testCreateNewFromMap_InvalidName()
{
const char* MAP_FILE =
"smallMapWithThreeSets.xml";
const char* NAME1 = "name/invalidname";
const char* NAME2 = "name/invalidname";
DataMapReader dmr;
DataMap dm = dmr.getDataMapFromFile(MAP_FILE);
CollectionFactory cf;
try
{
cf.createFromMap(NAME1, dm);
}
catch (std::exception const& e)
{
std::cerr << e.what() << std::endl;
}
/*CPPUNIT_ASSERT_THROW(cf.createFromMap(NAME1, dm), std::invalid_argument);
CPPUNIT_ASSERT_THROW(cf.createFromMap(NAME2, dm), std::invalid_argument);*/
}
对于每个请求,isNameValid的内容:
bool Collection::isNameValid(const std::string& name)
{
/* can't be blank */
if(name.length() == 0)
{
return false;
}
/* Can't contain '/' */
if(name.find('/') != std::string::npos)
{
return false;
}
return true;
}
发布于 2010-02-09 19:35:52
这是第一个Valgrind错误,还是之前的错误?
我的猜测是,有以前的问题,其中之一是破坏内存并导致抛出中断。
发布于 2010-02-17 05:28:06
Dave,在你的执行路径中有缺失的代码,这阻止了问题的解决:
constructor;
我建议在分段错误仍然可重现的情况下尽可能地剥离测试用例。
我对这个问题有以下猜测:
虽然这与问题中提到的分段错误无关,但同样的潜在问题也可能适用于下面在CollectionFactory::createFromMap()中抛出异常的代码:
如果输入参数(std::string和DataMap)的生存期小于集合实例的生存期,则
发布于 2010-02-14 17:39:26
你是如何链接代码的?例如,如果你正在创建一个共享库,你可能需要为位置无关的代码指定编译标志,比如-fPIC (gcc/g++)。
https://stackoverflow.com/questions/2231899
复制相似问题