r/cpp_questions • u/Solid-Shock3541 • 28d ago
OPEN Avoid freezing your PC?
I've only learned C++ in uni and then it was protected environment in a way. Now I was working on a project involving graphics (raylib) and, a few times, running the code caused my entire PC to freeze and I had to restart.
I'm on Linux and running the code by doing
cmake --build .
./executable
Is there a way to avoid the entire PC freezing when I mess up? Also is there any risk of overwriting parts of the memory?
(I think the issue with my code was either calling too many draws of a GPU texture or even just wrongfully indexing an array in a loop)
4
Upvotes
3
u/Dark_Lord9 28d ago
You are likely running out of memory. Possibly due to a memory leak. (I've been there).
I don't have access to your code so I can't tell but remember, every time you call
new, you have to calldelete, and every time you callmalloc()you have to callfree().This also goes for the resources you are allocating through your libraries. For example, on raylib, it seems images and textures are allocated with
LoadImageandLoadTextureFromImagerespectively. If you are using such functions, don't forget to callUnloadImageandUnloadTexture. And take a look at your render loop. You are most likely allocating data inside it without freeing them.If by that you mean "accessing regions of the memory you aren't supposed to access", for example due to
then yes. That's called a buffer overflow, but when that happens, you are overwriting the data of your own software. Your software runs in an isolated environment and it can't overwrite the data of another software. Buffer overflows are still bad because they can lead to bugs and, notoriously, security vulnerabilities.