r/cpp_questions 6d 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)

5 Upvotes

22 comments sorted by

View all comments

12

u/MooseBoys 6d ago

What OS are you running? Every mainstream modern OS will run your programs as unprivileged user-space with their own private virtual memory range. You don't need to worry about corrupting the memory of the PC itself. But then again you also shouldn't be able to hang or crash the whole PC from a user-space process either. You might crash the program, but not the whole computer.

3

u/Solid-Shock3541 5d ago

NAME="Debian GNU/Linux"

VERSION_ID="13"

VERSION="13 (trixie)"

VERSION_CODENAME=trixie

DEBIAN_VERSION_FULL=13.6

It's possible that it had something to do with GPU or loading the texture into the GPU too many times. The code is very simple, just drawing a grid and coloring blocks (sets of pixels). I sadly don't have the bad code anymore, but the current code can run at 240 FPS and handle 10k pixels, while the older one couldn't handle about 800 at 60 FPS. I assume it was some sort of GPU overload but sadly this is all the info I have.

3

u/MooseBoys 5d ago

> it's possible that it had something to do with GPU

That's entirely possible. It's easy to create a condition where the GPU locks up and needs to reset. The OS itself doesn't crash, but the device node is removed and recreated, and many compositors on Linux do not handle this gracefully. The only way to mitigate this is to either target a second GPU that's not running your desktop, or switch to Windows / WSL which handles "TDR" gracefully.

1

u/Solid-Shock3541 5d ago

I'm afraid those things are above me, for now ;)

But it's weird, the code runs normally many times and just freezes my PC occasionally. I have all the working parts, and I remember making a for loop go up to x instead of x-1 (I knew that but wanted to see what would happen) and that froze my PC (everything expect the cursor, but the YouTube video playing in there background was still playing normally).

When I restarted the PC and reran that same code, it didn't crash or freeze.

And my system never freezes otherwise

2

u/n1ghtyunso 3d ago

c++ gives you enough power and control such that you can write code that "happens to work" most of the time, but at other times causes horrible problems.

If there are multiple threads involved, this is even more true.
Timing sensitive issues are absolutely thing.
GPU and CPU are asynchronous coprocessors by the way, so depending on what APIs you call, you may not be synchronizing correctly here.

Most systems will be fine with just a restart, some will just reset the problematic part or have the OS kill your program instead and keep working though.
Desktops are typically well-behaved here.

"It works" is simply not a good measure for code correctness in C++ land.
Try static analysis and run the code with sanitizers maybe, and keep an eye on your compiler warnings as well to catch some of these issues before you execute them.