2
我在这一点上的代码调用auto_ptr的释放部件时,得到一个分段错误:段错误()
try
{
newMod->init(params);
}
catch (const std::exception& e)
{
#ifndef CONFIG_STATIC
dlclose(handle);
#endif
throw std::runtime_error(utils::buildString(
"%s: Error initializing module %s: %s",
DBG_FUNC_NAME, newMod->name().c_str(), e.what()));
}
_modules.insert(std::make_pair(newMod->name(), newMod.release()));
哪里_modules是
std::map<std::string, IModule*> _modules;
和NewMod的是
std::auto_ptr<IModule> newMod(0);
以后用适当的指针值重新设置。我知道指向IModule的指针是有效的,因为我在发布之前调用init成员。
此:
_modules.insert(std::make_pair(newMod->name(), newMod.get()));
newMod.release();
工作得很好,这是GDB说什么:
#0 _M_rep (this=0xbfe8e908, __str=...) at /usr/src/debug/gcc-4.5.1-20101208/obj-i586-suse-linux/i586-suse-linux/libstdc++-v3/include/bits/basic_string.h:287
#1 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (this=0xbfe8e908, __str=...)
at /usr/src/debug/gcc-4.5.1-20101208/obj-i586-suse-linux/i586-suse-linux/libstdc++-v3/include/bits/basic_string.tcc:173
#2 0x0805d76f in sm::core::mod::ModuleManager::loadModule (this=0x8074768, name=..., params=...) at src/core/ModuleManager.cpp:150
#3 0x08056edb in sm::core::Main::start (this=0xbfe8e9e0) at src/core/Main.cpp:82
#4 0x08055131 in main (argc=4, argv=0xbfe8ebf4) at src/core/smmain.cpp:15
,它出现segfaults上线是:
{ return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
什么可以在任何想法在这个错误?
同意这是相当危险的,即使它现在工作,也不保证在不同的编译器版本上工作 – EdChum
好的,所以这是正确的方法吗? '_modules.insert(std :: make_pair(newMod-> name(),newMod.get())); newMod.release();'我必须在这个中使用auto_ptr。 – Zbigh1
这应该有效,是的。 – Mat