r/cpp • • Jan 03 '26

Are memory leaks that hard to solve?

I have been coding in cpp for the last year (not regularly) and don’t have any professional experience. Why are memory leaks so hard to solve? If we use some basic rules and practices we can avoid them completely. 1) Use smart pointers instead of raw pointers 2) Use RAII, Rule of 5/3/0

I might be missing something but I believe that these rules shouldn’t cause memory related issues (not talking about concurrency issues and data races)

94 Upvotes

230 comments sorted by

View all comments

Show parent comment

82

u/[deleted] Jan 03 '26

[removed] — view removed comment

9

u/Professional_Top8485 Jan 03 '26

Yeah. Nasty thing that manifest as kernel killing your process when you want it least.

15

u/[deleted] Jan 03 '26 edited Jan 03 '26

[removed] — view removed comment

1

u/joahw Jan 04 '26

But the callstack on program end is pretty likely to be on a non-leaking allocation that happens to be the one that runs out of memory, right? Unless I am misunderstanding what you mean. But yeah digging through tagged memory dumps for anomalies is no fun regardless.

9

u/id3dx Jan 03 '26

Yep. I worked on a Windows project that had a C++ component and a dotnet component. The one memory leak the project ever ran into was due to the dotnet component not properly releasing registry resources.

0

u/Beentage Jan 03 '26

Garbage collection just removes memory management as being part of the main logic, but it’s part of the runtime.