I read cppreference and I see description of shared_ptr
The destructor of shared_ptr decrements the number of shared owners of the control block. If that counter reaches zero, the control block calls the destructor of the managed object. The control block does not deallocate itself until the std::weak_ptr counter reaches zero as well.
Okay, so when shared_ptr counter == 0 that control block calls destructor of managed object,
but as I can see in documentation make_shared
If any std::weak_ptr references the control block created by std::make_shared after the lifetime of all shared owners ended, the memory occupied by T persists until all weak owners get destroyed as well, which may be undesirable if sizeof(T) is large.
and here we have that Object (T
) which is stored by shared_ptr will be not deleted ( by calling destructor ). So, is it correct? I think instead of T
should be control block
Maybe the diffrence is that
- if I will use shared_ptr constructor that managed object will be destroyed when shared_ptr counter will be 0,
- if I will use make_shared that manage object will be destroyed when shared_ptr and weak_ptr counter will be 0.
Can someone clarify?
Aucun commentaire:
Enregistrer un commentaire