I am relatively new to shared_ptr. I'm using visual leak detector and I get errors at the end of the runtime when giving me following code as a memory leak :
std::shared_ptr<the_type>(new the_type(the_parameter))
Do i have to call something else like a deleter or even shared_ptr::reset()
, is it visual leak detector that is telling me a false leak or it is running before the shared_ptr
is actually being deleted?
Thanks.
note: i use vld 2.2.3, VS2012, Windows 7/8
There is nothing wrong with constructing a shared pointer in the way you showed. Looks like the leak detector is reporting fake leaks or you are using it in the wrong way. As a side note consider using std::make_shared
instead of explicit new
and shared pointer constructor - it is generally more efficient and safer.
Without more code, it's hard to say, but one clear possibility is that you have a cycle. Just using std::shared_ptr
everywhere is going to lead to problems sooner or later; it's a useful tool for specific cases, but it isn't going to solve all of your problems.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.