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)

95 Upvotes

230 comments sorted by

View all comments

Show parent comment

14

u/the_poope Jan 03 '26

You can also have cyclic references and thus memory leaks in Rust. But memory leaks aren't a security issue, it just causes the program to crash when you run out of memory.

5

u/mcfish Jan 03 '26

I'd dispute that last statement. It's likely platform dependent but if a system is out of memory, the OS may start killing processes. It might kill the leaking app, or some other process, since it probably has no way of knowing whether the growth in memory usage of your app is a leak or legitimate.

Similarly, I'd imagine on other OSes, your app hogging too much RAM could cause other processes to crash, or just not work properly, when they try and fail to allocate. So essentially a leaking application can cause denial of service to other services, which I would call a security issue.

-1

u/QuaternionsRoll Jan 03 '26

Memory leaks are generally not considered a memory safety issue because they cannot result in undefined behavior. You can exploit leaks to initiate a DoS attack, sure, but not much else.

2

u/meyriley04 Jan 03 '26

I mean it can be a security issue if the attacker intentionally exploits a memory leak crash to take down a specific system, no? Or just DoS