C++11: How is object deleted if it was constructed using make_shared
I am missing something about shared/weak pointers:
When a shared_ptr is constructed using make_shared, only one memory
allocation is used (to allocate memory for control block and object
itself). What happens if when last shared_ptr is destroyed but there are
weak_ptr-s left? At this point managed object has to be deallocated. But
if memory allocated by make_shared gets deallocated, that would make weak
pointers invalid, since same deallocation would destroy control block. I
guess realloc (to leave control block only) is impossible too - program
would need to keep track of all existing weak pointers and update them in
case realloc moved the pointer.
No comments:
Post a Comment