Opened 12 years ago

Closed 12 years ago

#5226 closed Bugs (invalid)

weak_ptr in program segfaults after expiring from shared_ptr from dynamically loaded shared library which was destructed and unloaded

Reported by: Peter Schüller <schueller.p@…> Owned by: Peter Dimov
Milestone: To Be Determined Component: smart_ptr
Version: Boost Release Branch Severity: Problem
Keywords: Cc:

Description

I have shared libraries which return shared_ptr<Foo> to my main program.

The main program creates weak_ptr<Foo> from these shared pointers.

Then the shared_ptr<Foo> objects are destructed (explicitly) and the shared library is unloaded.

After that, expired() calls to the weak_ptr<Foo> return true, destructing the weak_ptr<Foo> segfaults.

I have the suspicion that this is because weak_ptr uses a part of shared_ptr (this shared_count) that is supposed to survive the shared_ptr destruction. However, unloading the library invalidates the memory region where the code of that shared_count is located, so destructing the shared_weak_count fails?

Creating a minimal example is on my TODO list, but perhaps you can already tell me whether this is expected, and whether a fix is even possible without a major change to the weak/shared ptr architecture.

Thanks!

Change History (3)

comment:1 by Peter Schüller <schueller.p@…>, 12 years ago

Summary: weak_ptr in program segfaults after expiring from shared_ptr which was destructed by unloading shared libraryweak_ptr in program segfaults after expiring from shared_ptr from dynamically loaded shared library which was destructed and unloaded

fixed the wrong title of the issue

comment:2 by Peter Dimov, 12 years ago

This is expected, sorry. Creating the shared_ptr in the shared library instantiates a vtable, which is accessed when the last weak_ptr is destroyed. One workaround is to expose a C interface from the shared library:

T * create(); void destroy( T* );

and create the shared_ptr in an inline wrapper on the program side: shared_ptr<T>( create(), destroy ).

comment:3 by Peter Dimov, 12 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.