On Not | Mo Chit

January 05, 2005

Strange bug A very nasty bug had been plaguing me for the last week. Initially, I ignored it because it only happen on Wes' machine. However, today my machine caught the same bug (it's the flu season), and so I finally had to get to the bottom of it. Initially my machine would only cough violently every 1 out of 15 executions. I finally put in some print debug statements, and the bugger started happening every single time. After that it was just matter of stomping around for a little while until this little bugger was squashed.

The problem was that I was relying on Python's id function to generate unique ids, but I was accidentally throwing away the original object after I had made a deep copy of it. I technically didn't need the original object because I had the deep copy, but the ids weren't guaranteed to be unique anymore.

The reason why that can't be guaranteed to be unique is that Python uses the objects memory address for it's id. So, when the bug cropped up, what had happened was that the another object I was tracking had the same id as a previous object. The bug only showed up once in a while because the garbage collector had to reclaim and reuse that space for the bug to show up.

What's interesting is that inserting the print statements allowed the bug to reoccur every single time. I can only assume that by putting in the print statements, the garbage collector had enough time to reclaim the recently freed memory, and then give me another object that I was tracking at the exact same memory address.

I've got to make a unit test for this bad boy. I won't be repeating this mistake.

Creative Commons License
This site is licensed under a
Creative Commons License