Handmade Hero»Forums»Code
41 posts
[SOLVED] Weird bug: Rendering stops after 9987 frames and used RAM skyrockets without game crashing.
Edited by Opoiregwfetags on
Sorry for the long post, I have no idea what the cause of this weird bug might be, so I included many details just in case. I'm making a game inspired by HmH, in C++, on Windows, currently 22,000 LoC.

The problem seems weird to me: everything works fine, until a couple minutes go by, at which point the game graphics freeze. But the game doesn't crash, in fact programatically everything keeps functioning, and you can still hear the sounds respond to the input. I put a frame counter in the screen to see if the bug is consistent, and indeed every time the counter reaches 9987 the graphics stop.

Also, I noticed in the Task Manager that right after this, memory usage starts consistently going up by 3.3MB/s without stopping. More accurately, 53kB per frame, because when I change the framerate this seems to be kind of proportional to it. When I change the framerate, the bug still happens after 9987 frames.

I also tried disabling certain code blocks one by one with the debugger, for 1000 frames (by "frames" I mean game updates, which are coupled with frames) and then enabling them back, to see if the bug would still happen after 9987 frames. I disabled the part that sorts the render commands, the part that renders them, and the part that calls SwapBuffer(), separately and together, and every time the bug still happened after 9987 frames, which means the source of the problem lies elsewhere. I commented out the Input, the Sound and the dynamic saving/loading stuff, and still, after 9987 frames the graphics stop and the memory starts to go up by the same amount.

Maybe there's something in the game layer that's causing this? I disabled the game update call for two frames, and the counter, which lies in the game update, was 9985 when the graphics stopped, two less than usual, so it is not the cause. (Also, strangely, the memory usage started going up by 13kB/frame rather than 53kB). So it must be something inside the platform layer. What? I don't know. As I said, I commented out most stuff, there's not much there. It makes no sense to me. My only imaginable answer is that the WinAPI or OpenGL are doing something on the background implicitly...

I hope someone has some idea of what it could be, or can propose a general direction to investigate towards, otherwise I'll have to make a speedrun game haha. Here are some more details of the code: it has keyboard and mouse input via Windows API, gamepad input via XInput, audio via DirectSound, graphics through OpenGL, and I load some asset files with Windows API. The game layer is a dll that gets loaded, and also there's full-state saving/loading like there was in HmH. To draw graphics the game puts commands into a list that then gets passed back to the platform layer, and OpenGL goes through the list and draws. But whatever I disable, the graphics seem to stop after the main platform-layer loop has iterated 9987 times.
Mārtiņš Možeiko
2559 posts / 2 projects
[SOLVED] Weird bug: Rendering stops after 9987 frames and used RAM skyrockets without game crashing.
Edited by Mārtiņš Možeiko on
Open Task Manager and check under Details tab the count of "GDI Objects" for your process (you might need right click columns and choose "Select Columns" to add "GDI objects" there). Does this number go up all the time and does not go down?

Are you doing GetDC/ReleaseDC calls every frame? Or maybe you do only GetDC every frame without ReleaseDC? If yes - don't do that - call GetDC only once after you create window and never release HDC handle, reuse it for rest of the calls.
41 posts
[SOLVED] Weird bug: Rendering stops after 9987 frames and used RAM skyrockets without game crashing.
Indeed GDI Objects were going up, to a max of 10,000. I had forgotten a ReleaseDC() at one place. I put the appropriate ReleaseDC() and the problem went away, with GDI Objects stabilizing. Still, I did as you said and changed my multiple GetDC/ReleaseDC for a single, reused, DC. Thanks a lot!!!!!!!!!!!!!!