I've been reading about std::make_shared function lately. As far as I understand from Effective Modern C++ book, the make function should be preferred unless:
- Custom memory management is in place.
- There are memory concerns such as
std::weak_ptrmay outlive itsstd::shared_ptr, dealing with large objects, custom deleter, etc.
So, I understand std::make_shared allocates an object on the heap and its control block with just one call. Hence, the problem is std::shared_ptr's object may not be deleted until control block has to be released. Correct me if I am wrong, but this is when the last std::weak_ptr is released. Thus, std::make_shared may not be suitable if there is an alive std::weak_ptr which points to it.
But is this still a problem for most objects? Do we need to care when control block is released for non-large objects? Arguably, memory isn't that hard to get nowadays, so is this a concern for just large objects and low memory systems?
The point of my concern is: if I design a class which uses std::make_shared, and std::weak_ptr is used in the future, I have to go back and replace the make function with a regular std::shared_ptr<Class>(new Class()).
sizeof, so most types aren't large. The object is destroyed when the last livingshared_ptris destroyed; only the memory isn't released until the control block goes away. - T.C.