Not quite sure what is going on here. I am<br>serializing shared_ptr, that make use of <br>derivation from enable_shared_from_this.<br><br>However when my class is restored from<br>disk, it appears that accessing shared_from_this()<br> throws a tr1::bad_weak_ptr.<br>Using boost 1.42, gcc 4.2.1, on suse linux.<br><br>Has any one hit a similar issues, or found any<br>work arounds ? Any help greatly appreciated.<br><br>Here is some contrived standalone code, that demonstrate<br> the problem.<br><br>/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8<br>// =========== file Main.cpp ===================<br>// After restoring a class via shared_ptr it<br>// appears as if enable_shared_from_this has not<br> // been honoured. When accessing shared_from_this()<br>// I get tr1::bad_weak_ptr exception.<br><br>#include <iostream><br>#include <fstream><br>#include <assert.h><br><br>#include <boost/archive/text_iarchive.hpp><br> #include <boost/archive/text_oarchive.hpp><br>#include <boost/serialization/base_object.hpp><br>#include <boost/serialization/string.hpp><br>#include <boost/serialization/vector.hpp><br>#include <boost/serialization/shared_ptr.hpp><br> #include <boost/serialization/export.hpp><br><br>#include <boost/shared_ptr.hpp><br>#include <boost/enable_shared_from_this.hpp><br><br>using namespace std;<br>using namespace boost;<br><br>class Base :<br> ��� public boost::enable_shared_from_this<Base> ,<br>��� private boost::noncopyable {<br>protected:<br>���� Base() {}<br>��� virtual ~Base() {}<br>public:<br>� ��� virtual const std::string& name() const� = 0;<br> private:<br>��� friend class boost::serialization::access;<br>��� template<class Archive><br>��� void serialize(Archive & ar, const unsigned int){}<br>};<br>typedef boost::shared_ptr<Base> base_ptr;<br><br> <br>class Derived : public Base {<br>public:<br>��� Derived(const std::string& name ) <br>�������������� : Base(), name_(name){}<br>��� Derived() {}<br>��� virtual ~Derived() {}<br><br>��� virtual const std::string& name() const { return name_;}<br> <br>��� base_ptr find(const std::string& name) const {<br>��� ��� if (name_ == name) {<br>��� ��� ��� Derived* nonConstThis = const_cast<Derived*>(this);<br>��� ��� ��� return nonConstThis->shared_from_this(); // throws after restore ?<br> ��� ��� }<br>��� ��� return base_ptr();<br>��� }<br><br>private:<br>��� std::string name_;<br>��� friend class boost::serialization::access;<br>��� template<class Archive><br>��� void serialize(Archive & ar, const unsigned int) {<br> ������� ar & boost::serialization::base_object<Base>(*this);<br>������� ar & name_;<br>��� }<br>};<br>typedef boost::shared_ptr<Derived> d_ptr;<br><br>class Holder {<br>public:<br>��� Holder() {}<br>��� void add(d_ptr b) { vec_.push_back(b);}<br> <br>��� base_ptr find(const std::string& name) const {<br>��� ��� for(size_t i=0; i < vec_.size(); i++){<br>��� ��� ��� base_ptr base = vec_[i]->find(name);<br>��� ��� ��� if (base) return base;<br>��� ��� }<br> ��� ��� return base_ptr();<br>��� }<br>private:<br>��� std::vector<d_ptr> vec_;<br><br>��� friend class boost::serialization::access;<br>��� template<class Archive><br>��� void serialize(Archive & ar, const unsigned int){<br> �������� ar & vec_;<br>��� }<br>};<br><br>void save(const Holder& holder, const char* filename)<br>{<br>��� std::ofstream ofs( filename );<br>��� boost::archive::text_oarchive oa( ofs );<br>��� oa << holder;<br> }<br><br>void restore(Holder& holder, const char* filename)<br>{<br>��� std::ifstream ifs( filename );<br>��� boost::archive::text_iarchive ia( ifs );<br>��� ia >> holder;<br>}<br><br>int main()<br>{<br>��� std::string fileName = "test.txt";<br> <br>��� {<br>��� ��� Holder holder;<br>��� ��� holder.add( d_ptr(new Derived("me")));<br>��� ��� holder.add( d_ptr(new Derived("you")));<br><br>��� ��� base_ptr b = holder.find("me");<br>��� ��� assert(b.get()); // works<br> <br>��� ��� save(holder, fileName.c_str());<br>��� }<br><br><br>��� Holder restored;<br>��� restore(restored,fileName.c_str());<br><br>��� // why does the restored class throw<br>��� // a weak ptr exception<br>��� try {<br> ��� ��� base_ptr b = restored.find("me"); // fails ?<br>��� ��� assert(b.get()); // never gets here<br>��� }<br>��� catch (std::exception& e) {<br>��� ��� std::cout << "Ooops exception " << e.what()<br> ��� ��� << " thrown, test failed??? \n";<br>��� ��� std::remove(fileName.c_str());<br>��� ��� return 1;<br>��� }<br><br>��� std::remove(fileName.c_str());<br>��� cout << "test passed\n";<br> }<br clear="all"><br><br><br> � Best regards,<br>Ta,<br> � �Avi<br><br>