Why does this time_zone_ptr example not contain a memory leak?

I read through the accelerated DateTime library here , which contains many examples like this one:

time_zone_ptr zone(new posix_time_zone("MST-07")); 

I was curious why using the keyword 'new' does not cause a memory leak? I examined the source code for boost and noticed that it has two different versions of the constructor: one with shared_ptr and the other that uses weak_ptr. Can someone explain how this works and why this line is write-safe?

+4
source share
1 answer

time_zone_ptr is just an alias for boost::shared_ptr<time_zone> . This is a smart pointer that takes responsibility for a dynamically allocated object, from the raw pointer to which it is designed.

+7
source

Source: https://habr.com/ru/post/1488593/


All Articles