Finally I managed to draw the main menu in the screen.
I note two things:
- The message Demo is from a different game version! is being printed continuously
- If I start a new game, I got another crash related to accessing memory I don’t own.
The first one is easy to fix, searching for the error string I find it in file g_game.c at line 1592. With the debugger I find out that the WAD is expecting a game with version 109 and the code is reporting version 110. So is simple, just change the value in file doomdef.h at line 34.
Now, the next error is a harder to find out, again an error caused by assuming the target platform is 32 bits.
Problem appears in different places every time I run the game, for example one I can identify is located at p_setup.c in line 310. Now, here hell could be started. Previous issues regarding pointers were easy to fix because the heap of Z was empty/almost empty. Now at this point of the code a lot of memory have been requested to Z. I came out with a solution that isn’t elegant at all, but worked.
I modified memblock_t so it can store a string with the name of its owner, Z_Malloc() in z_zone.h and z_zone.c to receive the name of the owner and store it in the modified memblock_t and print it to a log file and finally also modified Z_Free() to print also when memory is freed, by who and where. Then I had to modify every call to Z_Malloc() and manually pass a string to identify the owner with the format: “variablename@filename:line”.
After reading with more detail the code in z_zone.h I realized there was a more elegant way for doing all this with macros but I’ve already made it my way so, who cares?
After all this I ran the game and checked where the problem was. After analyzing the stack when the game crashes I realize is because all the information in the “rover” used by Z to find free space in the heap is messed up. So I go verifying every entry in the output log and I find that the last variable that allocated memory decided to use more than it requested.
The variable is “linebuffer” in p_setup.c at line 537.
Again, the error is assuming a pointer has size of four bytes. This is easily fixed using the sizeof() operator. After applying this fix, the game runs without problem (still it throws a lot of garbage about sounds but that’ll be fixed later).
I published a branch with my ugly solution to dump the user of Z_Malloc().