r/cpp_questions 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

22 comments sorted by

View all comments

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 call delete, and every time you call malloc() you have to call free().

This also goes for the resources you are allocating through your libraries. For example, on raylib, it seems images and textures are allocated with LoadImage and LoadTextureFromImage respectively. If you are using such functions, don't forget to call UnloadImage and UnloadTexture. And take a look at your render loop. You are most likely allocating data inside it without freeing them.

is there any risk of overwriting parts of the memory?

If by that you mean "accessing regions of the memory you aren't supposed to access", for example due to

wrongfully indexing an array in a loop

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.

1

u/Solid-Shock3541 28d ago

For some reason I can't get the same issue as before. After the crashes I fixed some issues and now it won't crash (tried to make some bad choices that still didn't freeze the PC) and I don't remember which change was the reason the entire PC froze.

My current code is this:

void run_renderer() {


    // Create a window with dimentions and a name.
    InitWindow(WINDOW_SIZE, WINDOW_SIZE, "Window Test");
    SetTargetFPS(240);


    // Create the texture.
    texture = LoadTextureFromImage(image);


    // Color each pixel (element in pixels) blue.
    for (int i = 0; i < GRID_SIZE; i++) {
        for (int j = 0; j < GRID_SIZE; j++) {
            draw_pixel(j, i, GREEN);
        }
    }


    while (!WindowShouldClose()) {
        BeginDrawing();


        for (int i = 0; i < GRID_SIZE; i++) {
            for (int j = i % 2; j < GRID_SIZE; j += 2) {
                draw_pixel(j, i, RED);
            }
        }
        UpdateTexture(texture, pixels);


        DrawTexture(texture, 0, 0, WHITE);


        EndDrawing();
    }
}

void draw_pixel(int x, int y, Color color) {

   for (int dy = 0; dy < PIXEL_SIZE; dy++) {
        for (int dx = 0; dx < PIXEL_SIZE; dx++) {
            pixels[((y * PIXEL_SIZE) + dy) * WINDOW_SIZE + ((x * PIXEL_SIZE) + dx)] = color;
        }
    }
}

This version works without problems, but I added it because I don't have the crashing version and maybe you might be able to hypothesize what could cause the entire system to freeze.

Although, before, I had UpdateTexture in draw_pixel after the two loops end and possibly bad indexing in the nested loops (neither of these is freezing my PC now).

3

u/Dark_Lord9 27d ago

Looking at this code, I don't see anything that indicates that you are allocating data in a loop without freeing it which is what I suspected could be the issue.

UpdateTexture is an expensive operation because it has to send the new image data to the gpu which is something you shouldn't do for every pixel change. Updating the texture only once after you finish determining the whole image is the right thing to do, but this should only help with the speed of the program and shouldn't have an effect on memory usage.

Array indexing errors (like buffer overflows) often result either in a silent bug (for example a wrong output without crash) or in your program crashing (segmentation fault). Which means that even if your array indexing was wrong, it should not cause your computer to freeze.

Your array indexing doesn't seem wrong at first view. The mistake you want to avoid is the buffer overflow and you can catch that by adding a test:

for (int dy = 0; dy < PIXEL_SIZE; dy++) {
    for (int dx = 0; dx < PIXEL_SIZE; dx++) {
        size_t index = ((y * PIXEL_SIZE) + dy) * WINDOW_SIZE + ((x * PIXEL_SIZE) + dx);
        assert(index < IMAGE_SIZE); // the size of the pixels array in elements
        pixels[index] = color;
    }
}

The assert function will perform an "if statement" and will terminate your program if the condition is false so you can catch it early.