I have read that if we want to assign a shared_ptr to an already existing address of an object, then we need to define an empty custom deleter so that it is not tried to be deleted from heap. My question is if we have a shared_ptr object allocated using new and another shared_ptr object using an address, will there be any issue freeing the heap memory and maintaining reference counts etc?

some_class a;
shared_ptr<some_class> b(&a, custom_deleter);

shared_ptr<some_class> c(new some_class());
有帮助吗?

解决方案

Those shared_ptrs are completely independent. They both manage different objects and will use the appropriate deleter for those objects, so there are no issues with doing that.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top