c – 抛出异常会导致分段错误

前端之家收集整理的这篇文章主要介绍了c – 抛出异常会导致分段错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. Collection CollectionFactory::createFromMap(const std::string& name,const DataMap& dm) const
  2. {
  3. if (!Collection::isNameValid(name))
  4. {
  5. const std::string error = "invalid collection name";
  6. throw std::invalid_argument(error);
  7. }
  8. Collection c(name,dm);
  9. dm.initDataCollection(&c,true);
  10. return c;
  11. }

每当执行throw语句时,我都会遇到分段错误.这是Valgrind输出的原因.我不知道发生了什么事.

  1. ==21124== Invalid read of size 1
  2. ==21124== at 0x41D2190: parse_lsda_header(_Unwind_Context*,unsigned char const*,lsda_header_info*) (eh_personality.cc:62)
  3. ==21124== by 0x41D24A9: __gxx_personality_v0 (eh_personality.cc:228)
  4. ==21124== by 0x4200220: _Unwind_RaiseException (unwind.inc:109)
  5. ==21124== by 0x41D2C9C: __cxa_throw (eh_throw.cc:75)
  6. ==21124== by 0x4079BFB: corestore::CollectionFactory::createFromMap(std::string const&,corestore::DataMap const&) const (CollectionFactory.C:43)
  7. ==21124== by 0x8188F86: CollectionFactoryTest::testCreateNewFromMap_InvalidName() (CollectionFactoryTest.C:91)
  8. ==21124== by 0x81895D3: CppUnit::TestCaller<CollectionFactoryTest>::runTest() (TestCaller.h:166)
  9. ==21124== by 0x40D1BB5: CppUnit::TestCaseMethodFunctor::operator()() const (TestCase.cpp:34)
  10. ==21124== by 0x40C18E3: CppUnit::DefaultProtector::protect(CppUnit::Functor const&,CppUnit::ProtectorContext const&) (DefaultProtector.cpp:15)
  11. ==21124== by 0x40CD0FC: CppUnit::ProtectorChain::ProtectFunctor::operator()() const (ProtectorChain.cpp:20)
  12. ==21124== by 0x40CCA65: CppUnit::ProtectorChain::protect(CppUnit::Functor const&,CppUnit::ProtectorContext const&) (ProtectorChain.cpp:77)
  13. ==21124== by 0x40DC6C4: CppUnit::TestResult::protect(CppUnit::Functor const&,CppUnit::Test*,std::string const&) (TestResult.cpp:178)
  14. ==21124== Address 0xc82f is not stack'd,malloc'd or (recently) free'd

我已经进行了多次单元测试的重复轰炸,但是现在这是与其他所有相同的错误

  1. void CollectionFactoryTest::testCreateNewFromMap_InvalidName()
  2. {
  3. const char* MAP_FILE =
  4. "smallMapWithThreeSets.xml";
  5. const char* NAME1 = "name/invalidname";
  6. const char* NAME2 = "name/invalidname";
  7.  
  8. DataMapReader dmr;
  9. DataMap dm = dmr.getDataMapFromFile(MAP_FILE);
  10.  
  11. CollectionFactory cf;
  12. try
  13. {
  14. cf.createFromMap(NAME1,dm);
  15. }
  16. catch (std::exception const& e)
  17. {
  18. std::cerr << e.what() << std::endl;
  19. }
  20.  
  21. /*CPPUNIT_ASSERT_THROW(cf.createFromMap(NAME1,dm),std::invalid_argument);
  22. CPPUNIT_ASSERT_THROW(cf.createFromMap(NAME2,std::invalid_argument);*/
  23. }

每个请求,isNameValid的内容

  1. bool Collection::isNameValid(const std::string& name)
  2. {
  3. /* can't be blank */
  4. if(name.length() == 0)
  5. {
  6. return false;
  7. }
  8. /* Can't contain '/' */
  9. if(name.find('/') != std::string::npos)
  10. {
  11. return false;
  12. }
  13. return true;
  14. }

解决方法

这是第一个Valgrind错误还是以前的错误

我的猜测是有以前的那些,其中一个是破坏内存并导致抛出破坏.

猜你在找的C&C++相关文章