'What are the best strategy to address C++ memory leak errors?

I got a bunch of memory leak error warnings from Sonarqube for legacy C++ source code. From the first glance, it looks like majority of them are memory allocated on the heap and never got deleted, or delete was called instead of delete[].

My first thought was to replace the traditional heap allocation with smart pointers. However, I'd like to get everyone's feedback if there is anything I should watch out for with this approach.



Solution 1:[1]

If it is possible for you to move to newer versions of C++, then using Smart Pointers is definitely the way to go. One downside is that you will have to probably change a lot of code and decide which Smart Pointer to use for each use case. However, I think it will still be much less work than debugging all memory leak errors and fixing them.

Also, from a future maintainability point of view, using smart pointers is a good choice.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhishek Kumar