So I decided to be a smarty man and change ToeTag so that every piece of memory allocated would be handled by the garbage collector. I had several spots in the code where I was allocating standard C style memory chunks (via malloc and free) and that never sat right with me seeing as this is 2008 and all. I suppose when dealing with a game engine and data structures from over 10 years ago (Quake) it’s understandable but I wanted to do better.
I picked up a copy of the latest revision of Aaron Hillegass’s excellent book, Cocoa Programming for Mac OSX. Within it’s pages, I found the garbage collection section which was newly added as of this version of the book. It mentioned using the function call NSAllocateCollectable as a way of getting C style chunks of memory allocated that would be garbage collected. Marvelous, I thought! Unfortunately, either something is wrong or I’ve coded ToeTag in some retarded fashion that breaks this idea. ToeTag, when loading large maps, would allocate 2+ gigs of RAM and stall the machine until I killed the process. Just loading DM1 was taking upwards of 750 MB. Something was wrong.
So I ditched that idea and noticed a class called NSMutableData. This class gives you a beautiful object oriented way to allocate chunks of memory and get a pointer to that memory quickly and easily. Eureka! I changed all of the NSAllocateCollectable code to use this class instead and everything works great!
Not sure what was wrong with the former but the latter works perfectly. Now ToeTag no longer has those ugly C warts on it.
Tags: Misc