From: Seebs Date: 2008-06-30T15:47:19+09:00 Subject: Re: mysterious memory corruption, very confused On 2008-06-30, Roger Pack wrote: >> I get random data corruption when trying to execute queries. > valgrind might tell you if memory is being tramped. It is. There's a loop of VALUE x; char **foo = malloc(buncha char *); for (big list of things) { x = rb_obj_as_string(y); foo[i] = GetStringValue(x); } The idiom of using rb_obj_as_string, and then using the value, is common in the Ruby source. It works. ... It works *as long as you don't allocate anything more before you're done with it*. What ends up happening is that, if enough of the objects in question need a new string allocated by rb_obj_as_string, sooner or later you end up invoking the garbage collector. Now, since there's only one x, the garbage collector assumes the current rb_obj_as_string() return is in use, *and the others aren't*. So it might, if it wants the space, free one... And then the memory gets reused. With "big list" being about 12 items, about half of which needed new strings allocated, this ended up blowing up about once every thousand or two thousand runs. Unfortunately for me, I had about 80,000 data points. :) I submitted a more detailed bug report to the ruby-pg project, and I've adopted a workaround (possibly very inefficient) involving an array of VALUE objects and rb_gc_{un}register_address. It's ugly but it eliminates the bug. -- Copyright 2008, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!