
Kyle Girard wrote:
Hello,
I was fooling around with the boost::shared_ptr and boost::weak_ptr library and dynamic libraries and I received a segmentation fault which I do not fully understand and I am hoping someone can shed some light on the situation.
I am playing around with having objects in a dynamic library which are managed by shared_ptr's return weak_ptrs to the application My problem arises when the weak_ptrs aren't destroyed before the plugin is unloaded.
I was hoping that the weak_ptr would become invalid when the shared_ptr it points to is unloaded but instead it causes a segmentation fault.
What you're doing is reasonable and would be useful if it worked, but unfortunately, I can't think of a way to fix shared_ptr to support it. The problem is that when the dynamic library creates its shared_ptr<Foo>, the supporting class template boost::detail::sp_counted_base_impl_p<Foo> is instantiated along with its vtable, so the implementation of the virtual functions ends up in libFoo.so. When you unload libFoo.so, any attempt to call one of these virtual functions leads to a crash as they are no longer present. You can probably try to create a dummy shared_ptr<Foo>( new Foo ) in main.cc in order to force the main application to also contain the shared_ptr<Foo> code. This, of course, is not a general solution if main.cc doesn't know about Foo and only deals with base classes... but it might be enough for you.