Visual leak detector with std::shared_ptr

934 Views Asked by At

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

2

There are 2 best solutions below

3
On BEST ANSWER

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.

1
On

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.