Fwd: [serialization] is the weak_ptr parent kept inside an iarchive
I was running some tests involving std::weak_ptr using boost
serialization, and I realized that when I read a std::weak_ptr
from an archive that doesn't contain the "parent"
std::share_ptr, the behavior of the read pointer is "hard to
I am building and running this using g++-9.3.0 with
Clearly, the weak_ptr gets itself a shared_ptr "somewhere" as
it can be used after being read. I was wondering "where it is
stored and how to access it, if possible, without some
`weak.lock()` call ?"
It looks like the shared_ptr gets destroyed at the same time as
the archive, so I guess it is cleaned up thanks to the
delete_created_pointers method when the iarchive is destroyed :
does delete_created_pointers actually contains also shared_ptr ?
As far as I can see in the code serialization/weak_ptr.hpp, we
actually save then load a std::shared_ptr, so I was expecting
the weak_ptr to expire as soon as the call returns : since it is
not, there *is* another reference.
Now, I know I must save the shared_ptr in the archive myself to
have my own reference and to get all weak_ptr running properly.
But I came across this use case and I can't take it out of my
mind, so if you have an explanation, I'm all ears.