I was reading through the boost DateTime library here, which contains lots of examples such as this one:
time_zone_ptr zone(new posix_time_zone("MST-07"));
I was curious why the use of the keyword 'new' doesn't cause a memory leak? I investigated the boost source code, and noticed that it has two different versions of the constructor, one with a shared_ptr and another which uses a weak_ptr. Can someone explain to how these work, and why the above line is safe to write?
time_zone_ptr
is just an alias for boost::shared_ptr<time_zone>
. This is a smart pointer which takes ownership of a dynamically allocated object, from a raw pointer to which it is constructed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With