Profiling a C 3D engine using Valgrind

 I recently faced some memory corruption problems on my PATRIA 3D engine which are quite painful to be solved.

Luckily I started using Valgrind, an opensource dynamic analysis tool which can debug the memory allocation of a typical C program.

Valgrind is free and can be downloaded at this URL.

http://valgrind.org/

The very interesting part of Valgrind is that you can run your compiled code (complied using debug symbols BTW) and see what is going on at memory allocation/read/write and deallocation level.

This is really what a C programmer would live to have always in his toolbox.

 

For instance, it is quite easy and straightforward to find memory access errors such as:

==8334== Invalid read of size 1

==8334==    at 0x4C29804: strlen (mc_replace_strmem.c:282)

==8334==    by 0x42FF06: PATRIA_Shaders_LoadAndCompile (PATRIA_Shaders.c:113)

==8334==    by 0x430D20: PATRIA_Shader_Load_Phong (PATRIA_Shaders.c:649)

==8334==    by 0x4137A4: Game_Menu_Engine_Init (CUBE_Game_Menu.c:3260)

==8334==    by 0x432657: main (main.c:244)

 

 

 

It is also quite reliable the function which identifies the conditional jumps in the program which are based on uninitialized values such as:

==8216== Conditional jump or move depends on uninitialised value(s)

==8216==    at 0x41761B: PATRIA_Draw_Scene (PATRIA_CUBE_Draw.c:1801)

==8216==    by 0x40D195: Game_Engine_DO (CUBE_Game_Logic.c:4563)

==8216==    by 0x50A93F7: glutMainLoop (glut_event.c:972)

==8216==    by 0x4326A8: main (main.c:267)

Valgrind allows you to identify the code line subject of the problem (in the last example the line 1801) and the exact file name where the line belongs to (PATRIA_CUBE_Draw.c).

As it is quite natural for a debugger like valgrind, it reports as well the call stack of the program:

 

– main

      – glutmainloop

             – Game_Engine_DO

                      -PATRIA_Draw_Scene

 

I am very satisfied about this tool, it is free, well done and very realiable.

 

Good work guys.

 

Leave a comment