3
使用clang(3.5.1)和地址清理器在我的程序中使用boost(1.56)我得到: boost/serialization/singleton.hpp:132:13:运行时错误:引用绑定以空指针boost序列化单例和消毒剂:空引用
的例子是:
#include<boost/serialization/singleton.hpp>
#include <string>
#include <iostream>
class Foo{
private:
std::string bar = "Hello World";
public:
void print() const{
std::cout << bar << std::endl;
}
};
int main(){
boost::serialization::singleton<Foo> test;
test.get_const_instance().print();
}
然后我做的:
编译
clang++ -I/boost/1_56_0/gcc-4.8.2/include/ -fsanitize=address,undefined -std=c++11 test.cpp
输出
./a.out:
boost/1_56_0/gcc-4.8.2/include/boost/serialization/singleton.hpp:132:13: runtime error: reference binding to null pointer of type 'const Foo'
Hello World
看代码,我由参考实例在单类的角色混淆。它看起来像未定义的行为。 你明白了吗?
template<class T>
bool detail::singleton_wrapper<T>::m_is_destroyed = false;
} // detail
template <class T>
class singleton : public singleton_module
{
private:
BOOST_DLLEXPORT static T & instance;
// include this to provoke instantiation at pre-execution time
static void use(T const &) {}
BOOST_DLLEXPORT static T & get_instance() {
static detail::singleton_wrapper<T> t;
// refer to instance, causing it to be instantiated (and
// initialized at startup on working compilers)
BOOST_ASSERT(! detail::singleton_wrapper<T>::m_is_destroyed);
use(instance); // That's the line 132
return static_cast<T &>(t);
}
public:
BOOST_DLLEXPORT static T & get_mutable_instance(){
BOOST_ASSERT(! is_locked());
return get_instance();
}
BOOST_DLLEXPORT static const T & get_const_instance(){
return get_instance();
}
BOOST_DLLEXPORT static bool is_destroyed(){
return detail::singleton_wrapper<T>::m_is_destroyed;
}
};
问题很可能不在升压代码中,但在您如何使用它。请编辑您的问题以显示*您的*代码,最好是[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 – 2015-01-21 08:59:42
公平。我准备好了。 – 2015-01-21 09:00:54
我会尝试将它进一步煮沸 - 您可以在不使用Boost的情况下制作repro案例,如果您需要针对这些工具提交错误,这将会很有帮助。 – 2015-01-21 09:02:12