Memory leak detection design pattern for reference-counted smart pointers

We have our own class of smart pointers, which is counted using the base AddRef and Release.

During debugging, I can see that a lot of objects are not released properly. I see which objects are not released, but it is difficult to know from what place they stand out.

Is there a good design pattern to solve this problem?

I set the debug statements and ADDREF and RELEASE, and I can see the reference counter when calling release. In some cases, it is more than 1, that is, the pointer is not deleted.

If I start placing breakpoints on shared_ptr const, then it will be called hundreds of times and it is difficult to detect a memory leak.

It appears that circular links are being created that cause these leaks.

0
source share
2 answers

Do not use design patterns for this. Do not add code bloat for leak detection only.

Use a memory tool. Valgrind comes to mind as well as a number of commercials for Windows.

Also...

I see which objects are not released, but it is difficult to know from what place they stand out.

Why? Set a breakpoint in the constructor of the object. Instead, use standard pointers ( std::shared_ptr , std::unique_ptr ).

According to your changes:

I set the debug statements and ADDREF and RELEASE, and I can see the reference counter when calling release. In some cases, it is more than 1, that is, the pointer is not deleted.

Do you take into account elision copying (RVO / NRVO)? This is most likely the cause.

+2
source

A few years ago, before auto_ptr , I wrote my own class of smart pointers, which we widely used in our code base. Of course, this was a complete push for errors.

I had exactly the same problem. Although I canโ€™t show you the actual code that I used to identify call sites that experienced resource leaks (since the code no longer exists), I can tell you what I did.

The first thing I wrote was the #define > SHUDDER <macro, similar to the one I set out here , which I could use instead of new to highlight my objects. The macro sent additional parameters to the smart pointer, indicating the file number and call site number. A smart pointer will save that.

Then I replaced the global new my macro when I cast the #defines magic spell that included this feature. Obviously, this should be disabled in all Releaseasse builds and only used in Debug builds when you are really debugging it.

Then I run my program until I get to the point where I would like to see all the call sites. I would execute the dump_call_sites() method on a smart pointer that dumped the static vector lines of the call site lines to standard output. Thus, the problem found, I would return all these magic spells to disable all this pokus hokus.

This is really code encryption. This is far from elegant, and he introduces his own set of problems into it. But along with debugging printf it has its place. If you donโ€™t want or cannot go downloading your Rational Purify or Bounds Checker product, this can help you quickly identify where the leaks are coming from.

+1
source

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


All Articles