Using clang (3.5.1) with address sanitizer on my program using boost (1.56) I got: boost/serialization/singleton.hpp:132:13: runtime error: reference binding to null pointer
The example is:
#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();
}
Then I do:
compilation
clang++ -I/boost/1_56_0/gcc-4.8.2/include/ -fsanitize=address,undefined -std=c++11 test.cpp
output
./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
Looking at the code, I am confused by the role of the reference instance in the class singleton. It looks like undefined behaviour. Do you get it?
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;
}
};