Re: mysterious memory corruption, very confused



On Jul 6, 2008, at 7:15 PM, Nobuyoshi Nakada wrote:

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.


I am wondering why the strings (returned from rb_obj_as_string) will
be garbage collected but the array will not be garbage collected? Both
have the same local scope, and they are not referenced by any other
ruby object.

Please explain when you have time.

Blessings,
TwP

.



Relevant Pages

  • Re: mysterious memory corruption, very confused
    ... It works *as long as you don't allocate ... sooner or later you end up invoking the garbage collector. ... adopted a workaround involving an array of VALUE ... It's ugly but it eliminates the bug. ...
    (comp.lang.ruby)
  • Re: transform byte[] in IntPtr or int
    ... AddrOfPinnedObject() of arrays. ... The bug is fixed in v2 of .NETCF and may be ... Instead of using a managed array, allocate the memory using a native API ... Environment.Version to determine what version of .NETCF you are running on, ...
    (microsoft.public.dotnet.framework.compactframework)
  • PROBLEM: 2.6.0-test10 BUG/panic in mpage_end_io_read
    ... backup folders from half of a failed raid 1 array. ... but the third one causes a BUG or panic every time. ... hdds that are going bad spew out a lot ... 8800-88ff: libata ...
    (Linux-Kernel)
  • Re: undefined vs. undefined (was: new Array() vs [])
    ... After thinking it over again tonight, I see no bug in Mozilla. ... Quote 1 ... Elided array elements are not defined. ... var p = br + br; ...
    (comp.lang.javascript)
  • Re: c / c++ : is it end of era ?
    ... You have to scan the whole string first ... has taken the trouble to learn the language. ... This is not a bug, ... array, however, is converted into a pointer to the array's first element ...
    (comp.lang.c)