Re: mysterious memory corruption, very confused



Hi,

At Mon, 30 Jun 2008 15:47:19 +0900,
Seebs wrote in [ruby-talk:306636]:
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);
}

It's your bug.

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.

Because you drop the references to the created objects. You
have to keep the objects but not only the pointers.

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.

VALUE x, array;
char **foo = malloc(buncha char *);

for (big list of things) {
x = rb_obj_as_string(y);
rb_ary_push(array, x);
foo[i] = GetStringValue(x);
}

By keeping the values in an automatic variable `array', they
are marked and won't be freed.

--
Nobu Nakada

.



Relevant Pages

  • Re: mysterious memory corruption, very confused
    ... It's your bug. ... the Ruby source. ... Now, since there's only one x, the garbage collector assumes the ... VALUE x, array; ...
    (comp.lang.ruby)
  • Re: Array allocation and garbage collection
    ... then use the array, then at some later point, allocate it again, eg: ... does the garbage collector know that the memory allocated the first time round can be garbage ... the garbage collector is smart enough to know that if nothing ... currently refers to the array, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Isnt there anything like garbage collector in C?
    ... I was thinking I could start by storing the first number in an array ... in a new array with size two and delete the first array from memory. ... Isn't there anything like garbage collector in C? ... As for your program design, if you know that you are going to input twenty numbers, why don't you allocate space for twenty numbers to begin with? ...
    (comp.lang.c)
  • Re: Isnt there anything like garbage collector in C?
    ... I was thinking I could start by storing the first number in an array ... Isn't there anything like garbage collector in C? ... Yes, you can delete things from memory yourself, but only if you created ... why don't you allocate space for twenty numbers to begin ...
    (comp.lang.c)
  • Re: Reading an Ascii string
    ... The garbage collector handles the deallocation, and also moves allocated objects so that the memory doesn't get fragmented. ... To allocate and release objects is faster in a garbage collected environment than in a traditional heap that uses reference counters. ... If you are using a StringBuilder anyway, there is an override of the Append method that takes a char array, so then you wouldn't need the step of creating the string from the array. ...
    (microsoft.public.dotnet.languages.csharp)