Using Valgrind with C-API on Linux

If you've got a nasty pointer issue, or just want to check. It's possible to debug your game on Linux with the venerable executable-checker Valgrind.

First you need to build your project in debug mode. This allows valgrind to tell you the line number of the issue (rather than just the function name).

cmake -DCMAKE_BUILD_TYPE=Debug .

And then run valgrind, specifying the full path to the simulator executable:

valgrind ./PlaydateSDK/PlaydateSDK-1.12.3/bin/PlaydateSimulator MyAwesomeGame.pdx

There is a bunch of spurious errors that come up, but just ignore these until your game starts playing. If these really annoy you, it's possible to make an "ignore list" (I can't recall the exact valgrind terminology for this list off the top of my head, but it's in the doco).

The simulator runs really slowly through valgrind, but it mostly works. Sometimes (for me on a battery-powered laptop) it had periods of missed key-presses, but this wasn't a deal-breaker.

And issues will be made clear:

==28528== Conditional jump or move depends on uninitialised value(s)
==28528== at 0x484690B0: ___displaceAnimatedSprite (animated_sprite.c:194)
==28528== by 0x4846923D: updateAnimatedSpriteWithCollisions (animated_sprite.c:237)
==28528== by 0x4846768C: updateBomb (main.c:582)
==28528== by 0x467D86: pc_sprite_updateSprites (in /home/sando/Code/Play.Date/PlaydateSDK/PlaydateSDK-1.12.3/bin/PlaydateSimulator)

1 Like