Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this time_zone_ptr example contain a memory leak?

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?

like image 729
hershey101 Avatar asked Mar 24 '23 09:03

hershey101


1 Answers

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.

like image 55
Kerrek SB Avatar answered Apr 02 '23 08:04

Kerrek SB