2015-01-21 28 views
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; 
    } 
}; 
+1

问题很可能不在升压代码中,但在您如何使用它。请编辑您的问题以显示*您的*代码,最好是[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 – 2015-01-21 08:59:42

+0

公平。我准备好了。 – 2015-01-21 09:00:54

+0

我会尝试将它进一步煮沸 - 您可以在不使用Boost的情况下制作repro案例,如果您需要针对这些工具提交错误,这将会很有帮助。 – 2015-01-21 09:02:12

回答

0

我无法得到(地址?)消毒工作在我的Xcode 6环境中。但是我通过调试器跟踪了程序。 singleton.hpp的线132包含行

use(instance);

其中实例的值具有(零初始化值)。这可能被消毒剂视为错误,但使用(...)是一个空功能。它只包含在保证单例在main之前被调用。如果不包含这些,编译发布可能会优化主前调用,并且类可能无法按预期运行。所以我会称这是一个过度热情的消毒剂行为。或者,也许消毒剂可能被认为不够智能以追踪更深层次的水平。要么... 。