2 ms·
Every time I read of memory leaks in C++ codebase, I get no idea why RAII didn't work in those cases. Even if the codebase was huge and complex. Circular refere
by habibur 4y ago
Every time I read of memory leaks in C++ codebase, I get no idea why RAII didn't work in those cases. Even if the codebase was huge and complex. Circular references? Some special cases why one can't use RAII?
Forget unique pointer or smart pointers. People had been coding in C++ far before that without fearing memory leaks by following RAII principals.
- sidlls 4y agoRAII isn't a perfect safeguard against memory leaks. Rust doesn't provide 100% protection against them, either, especially when `unsafe` gets involved. It's better than C++, though, in this regard. There is no fool-proof way to avoid them, other than by never dynamically allocating memory.
- Shorel 4y agoIf everything is stack allocated, then the never dynamically allocating memory part will be true.
- crabbone 4y agoUnless you are very deep in system / embedded, most of the code that constitutes your program is not your code -- it's various libraries, frameworks, code generated by utilities etc. Often times this is the source of both unsafe interfaces and misunderstanding of the functionality on the part of the developer writing the code. Second to it is, probably, concurrency. RAII isn't going to save you here either. Then there's also input processing where you may run into a situation that causes resources not being de-allocated because you never anticipated a particular shape of the input. RAII works for very local and very well-defined things, it's not a good answer to dynamic situations or resources with complicated ownership.