Tuesday, August 27, 2013

I Remember Nothing

I can shut down cleanly with no memory leaks!  Whoo!

OK, not that big a deal... well, unless you've ever tried to make your game do it, and then maybe you can appreciated it.  :)  At least, it's the most I can mange through the fog of whatever it is that's ailing me.

Since I'm currently working on PC, this was made somewhat easier by using the Windows debug allocators:


_aligned_malloc_dbg
_aligned_free

I'm only using the aligned versions - it makes things a lot easier to just have everything 16-byte aligned (required for SIMD instructions and occasionally other things).  Most allocations where this would make any difference at all go into a container or block allocator anyway.

In any case, these functions have one fairly nifty feature:  _crtBreakAlloc.


When you exit your game, you will get a list of memory allocations that haven't been cleaned up, along with an allocation number associated with each one (and hopefully the file and line where the allocation happened).

So, if the 1065th allocation was not released, single-step to start the game again, put _crtBreakAlloc in a watch window, set it to 1065, and continue to run the game.  When the 1065th allocation happens, you will get a break, and hopefully you can figure out why that particular allocation was not released.

This technique doesn't work once you have allocations happening in a non-deterministic order - say, after user input or random number generation - but for testing purposes it should be possible to work around those.

No comments:

Post a Comment