The Geek, The Bug, and The Sledgehammer
Jun. 17th, 2005 10:01 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
For the past day or so, I have had a conundrum. Freespace 2 kept crashing on me at the end of one particular mission. It didn't seem to matter how I terminated the mission so long as I didn't quit out.
So, I geeked. Freespace runs on top of X, the windowing environment for Unix. Because the windowing environment, the window manager, the desktop environment, and client programs are all independent layers, I took my laptop downstairs and proceeded to log in remotely to my desktop, then ran Freespace 2 in the debugger (gdb), with the output to my laptop. I had two screens open connected by the wireless network-- in one, I was playing the game, and in the other I was watching a real-time display of memory usage and code patterns. Very cool.
The leak turned out to be an attempt to release memory already released. I couldn't figure out how that was possible since, according to the code I saw, it was checking to make sure it did not double-release memory, so why was it trying to free something marked as already freed?
Annoyed, and wanting to play the game, I pulled out a programmer's sledge hammer. Hans Boehm's garbage collector is a library that allows you to comment out all memory freeing; instead, it routinely checks your memory tree and frees the stuff you're not using. I recompiled the game with the library enabled, having substituted the GC_MALLOC call for the routine memory allocation call.
It worked. And it was fascinating to watch my memory patterns go up and down in reliable, routine fashions. The garbage collector was freeing memory once and only once, and my game ran without a glitch. Heck, I think the performance numbers were even a little better. And it was nice to code in C, if only for a little bit. I've been wrapping my head around Javascript's lisp-like enclosures and the double-secret-wacky way object orientation is done, and I want something simple, like C, once in a while to clear my head.
So, I geeked. Freespace runs on top of X, the windowing environment for Unix. Because the windowing environment, the window manager, the desktop environment, and client programs are all independent layers, I took my laptop downstairs and proceeded to log in remotely to my desktop, then ran Freespace 2 in the debugger (gdb), with the output to my laptop. I had two screens open connected by the wireless network-- in one, I was playing the game, and in the other I was watching a real-time display of memory usage and code patterns. Very cool.
The leak turned out to be an attempt to release memory already released. I couldn't figure out how that was possible since, according to the code I saw, it was checking to make sure it did not double-release memory, so why was it trying to free something marked as already freed?
Annoyed, and wanting to play the game, I pulled out a programmer's sledge hammer. Hans Boehm's garbage collector is a library that allows you to comment out all memory freeing; instead, it routinely checks your memory tree and frees the stuff you're not using. I recompiled the game with the library enabled, having substituted the GC_MALLOC call for the routine memory allocation call.
It worked. And it was fascinating to watch my memory patterns go up and down in reliable, routine fashions. The garbage collector was freeing memory once and only once, and my game ran without a glitch. Heck, I think the performance numbers were even a little better. And it was nice to code in C, if only for a little bit. I've been wrapping my head around Javascript's lisp-like enclosures and the double-secret-wacky way object orientation is done, and I want something simple, like C, once in a while to clear my head.
no subject
Date: 2005-06-18 04:16 pm (UTC)no subject
Date: 2005-06-18 05:59 pm (UTC)no subject
Date: 2005-06-18 06:06 pm (UTC)The weirdo thing about gcc is that -g -O2 is perfectly legit... after all these years, having been raised on Berkeley and AT&T compilers where that was verboten, it still feels weird...
no subject
Date: 2005-06-24 04:40 am (UTC)There are some tricks you can play with the allocator to reduce the likelihood of false roots. It helps if you don't have large linked structures where one false root can prevent large numbers of objects from being collected.
no subject
Date: 2005-06-19 03:36 pm (UTC)no subject
Date: 2005-06-20 04:50 am (UTC)This wasn't a "not free" problem; this was a "free twice" problem, even though the line right above it does a check to make sure the pointer is not null.
no subject
Date: 2005-06-20 02:43 pm (UTC)but i think valgrind can find those kinds of errors too. free twice errors are very unpredictable. I didn't know they caused memory leaks.
no subject
Date: 2005-06-19 05:43 pm (UTC)Someday...