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;
}
Whenever the throw statement is executed, I'm getting a segmentation fault. Here is the cause from Valgrind output. I've no idea what's going on.
==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
I've had several iterations of the unit test that's bombing, but here is the current one that exhibits the same bug as all the others:
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);*/
}
Per request, the contents of 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;
}
In practice, segfaults are almost always due to trying to read or write a non-existent array element, not properly defining a pointer before using it, or (in C programs) accidentally using a variable's value as an address (see the scanf example below).
They are both called exceptions, but they originate at different levels of the software/hardware of the system. Technically, you can catch segfaults with a signal handler for SIGSEGV . However, as Ivaylo explains, it's is not, typically, allowed to just "try again" if you get a segfault.
See if your compiler or library can be set to check bounds on [i] , at least in debug mode. Segmentation faults can be caused by buffer overruns that write garbage over perfectly good pointers. Doing those things will considerably reduce the likelihood of segmentation faults and other memory problems.
Rarely, a segmentation fault can be caused by a hardware error, but for our purposes we will be taking a look at software-based memory access errors. Although, segmentation faults are the result of invalid memory access, incorrect access to memory will not always result in a segmentation fault.
Is that the very first Valgrind error or are there previous ones?
My guess is that there are previous ones and one of those is corrupting memory and causing throw to break.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With