# Strange rb\_gc\_mark error

**URL:** <https://rubytalk.org/t/strange-rb-gc-mark-error/9318>\
**Category:** ruby-talk\
**Created:** [13 December 2003 12:06 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318 "2003-12-13T12:06:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elias\_Athanasopoulos](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@Elias\_Athanasopoulos](https://rubytalk.org/u/Elias_Athanasopoulos)\
**Post date:** [13 December 2003 12:06 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/1 "2003-12-13T12:06:54Z")

</div>

Hello!

I have a pure C function, which may be called arbitrary  
sequential times. If I call any rb\_\* function inside, including  
the simple rb\_warn(), I get:

BUG] rb\_gc\_mark(): unknown data type 0x28(0x89965b0) non object  
ruby 1.8.0 (2003-08-04) [i586-linux]

The error occures after a lot of calls; it reminds me the  
behaviour of a stack overflow, or something.

The C function is quite simpe:

static double foo (double \*x, double \*y)  
{  
double f = 5.000000;

```
rb_warn ("%f\n", f);

return f;

```

}

If I remove the rb\_warn() call, everything works okay.

Any hints?

Regards,

> **···**
>
> –  
> University of Athens I bet the human brain  
> Physics Department is a kludge --Marvin Minsky

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [13 December 2003 13:11 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/2 "2003-12-13T13:11:12Z")

</div>

Hi,

> **···**
>
> In message “strange rb\_gc\_mark error” on 03/12/13, Elias Athanasopoulos [elathan@phys.uoa.gr](mailto:elathan@phys.uoa.gr) writes:
> 
> > I have a pure C function, which may be called arbitrary  
> > sequential times. If I call any rb\_\* function inside, including  
> > the simple rb\_warn(), I get:
> > 
> > BUG] rb\_gc\_mark(): unknown data type 0x28(0x89965b0) non object  
> > ruby 1.8.0 (2003-08-04) [i586-linux]
> > 
> > The error occures after a lot of calls; it reminds me the  
> > behaviour of a stack overflow, or something.
> 
> Show us whole program, if possible, to reproduce your error.
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Elias\_Athanasopoulos](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@Elias\_Athanasopoulos](https://rubytalk.org/u/Elias_Athanasopoulos)\
**Post date:** [13 December 2003 15:02 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/3 "2003-12-13T15:02:27Z")

</div>

I will try, but it is a little bit hard because the C func is called  
from a C++ method deeply inside a complex inheritance tree.

Things that I can verify now are:

(a) The C func is called with no problem 8000 times.  
(b) The C func is called for 3000+ and segfaults if I put a  
simple rb\_warn() inside. Ruby 1.8.0  
(c) The C func is called for 6000+ and segfaults if I put a  
simple rb\_warn() inside. Ruby 1.8.1 preview 3.

Regards,

> **···**
>
> On Sat, Dec 13, 2003 at 10:11:12PM +0900, Yukihiro Matsumoto wrote:
> 
> > Hi,
> > 
> > In message “strange rb\_gc\_mark error” \> on 03/12/13, Elias Athanasopoulos [elathan@phys.uoa.gr](mailto:elathan@phys.uoa.gr) writes:
> > 
> > > I have a pure C function, which may be called arbitrary  
> > > sequential times. If I call any rb\_\* function inside, including  
> > > the simple rb\_warn(), I get:
> > > 
> > > BUG] rb\_gc\_mark(): unknown data type 0x28(0x89965b0) non object  
> > > ruby 1.8.0 (2003-08-04) [i586-linux]
> > > 
> > > The error occures after a lot of calls; it reminds me the  
> > > behaviour of a stack overflow, or something.
> > 
> > Show us whole program, if possible, to reproduce your error.
> 
> –  
> University of Athens I bet the human brain  
> Physics Department is a kludge --Marvin Minsky

---

<div class="post-metadata">

**Author:** ![Elias\_Athanasopoulos](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@Elias\_Athanasopoulos](https://rubytalk.org/u/Elias_Athanasopoulos)\
**Post date:** [13 December 2003 17:33 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/4 "2003-12-13T17:33:04Z")

</div>

Okay, it is my fault. ☹

While writting a C++ extension for Ruby, I do (almost everywhere):

static VALUE foo\_init (…)  
{  
Foo \*f = new Foo();  
rb\_iv\_set (self, “ **ptr** ”, (VALUE) f);  
return self;  
}

static VALUE foo\_bar (…)  
{  
Foo \*f = (Foo \*) rb\_iv\_get (self, “ **ptr** ”);  
f-\>Bar();  
return self;  
}

The error ruby reports is on an address of a Foo created pointer.  
If I remove the rb\_iv\_set() call, ruby executes correctly. But, I  
have to keep the pointer somewhere in order to execute methods of  
the specific class.

Any ideas?

Regards,

> **···**
>
> On Sun, Dec 14, 2003 at 12:02:27AM +0900, Elias Athanasopoulos wrote:
> 
> > On Sat, Dec 13, 2003 at 10:11:12PM +0900, Yukihiro Matsumoto wrote:
> > 
> > > Hi,
> > > 
> > > In message “strange rb\_gc\_mark error” \> \> on 03/12/13, Elias Athanasopoulos [elathan@phys.uoa.gr](mailto:elathan@phys.uoa.gr) writes:
> > > 
> > > > I have a pure C function, which may be called arbitrary  
> > > > sequential times. If I call any rb\_\* function inside, including  
> > > > the simple rb\_warn(), I get:
> > > > 
> > > > BUG] rb\_gc\_mark(): unknown data type 0x28(0x89965b0) non object  
> > > > ruby 1.8.0 (2003-08-04) [i586-linux]
> > > > 
> > > > The error occures after a lot of calls; it reminds me the  
> > > > behaviour of a stack overflow, or something.
> > > 
> > > Show us whole program, if possible, to reproduce your error.
> > 
> > I will try, but it is a little bit hard because the C func is called  
> > from a C++ method deeply inside a complex inheritance tree.
> > 
> > Things that I can verify now are:
> > 
> > (a) The C func is called with no problem 8000 times.  
> > (b) The C func is called for 3000+ and segfaults if I put a  
> > simple rb\_warn() inside. Ruby 1.8.0  
> > (c) The C func is called for 6000+ and segfaults if I put a  
> > simple rb\_warn() inside. Ruby 1.8.1 preview 3.
> 
> –  
> University of Athens I bet the human brain  
> Physics Department is a kludge --Marvin Minsky

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [13 December 2003 17:37 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/5 "2003-12-13T17:37:08Z")

</div>

> Any ideas?

Use Data\_Wrap\_Struct

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Derek\_Lewis](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@Derek\_Lewis](https://rubytalk.org/u/Derek_Lewis)\
**Post date:** [13 December 2003 18:23 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/6 "2003-12-13T18:23:10Z")

</div>

I don’t know if it makes a difference, but in my libxosd-ruby code I’m  
doing something similar, but it seems to work fine:

xosd \*osd;  
VALUE r\_osd;

osd = xosd\_create(lines);  
r\_osd = INT2FIX( (int)osd );

rb\_iv\_set(self, “@osd”, r\_osd);

Two major differences I notice:

1. I’m using INT2FIX instead of casting to VALUE
2. I’m using an “@” on my variable name in rv\_iv\_set

Also, a quick question. Am I creating objects that won’t be GC’d by using  
INT2FIX like that?

> **···**
>
> On Sun, 14 Dec 2003, Elias Athanasopoulos wrote:
> 
> > On Sun, Dec 14, 2003 at 12:02:27AM +0900, Elias Athanasopoulos wrote:
> > 
> > > On Sat, Dec 13, 2003 at 10:11:12PM +0900, Yukihiro Matsumoto wrote:
> > > 
> > > > Hi,
> > > > 
> > > > In message “strange rb\_gc\_mark error” \> \> \> on 03/12/13, Elias Athanasopoulos [elathan@phys.uoa.gr](mailto:elathan@phys.uoa.gr) writes:
> > > > 
> > > > > I have a pure C function, which may be called arbitrary  
> > > > > sequential times. If I call any rb\_\* function inside, including  
> > > > > the simple rb\_warn(), I get:
> > > > > 
> > > > > BUG] rb\_gc\_mark(): unknown data type 0x28(0x89965b0) non object  
> > > > > ruby 1.8.0 (2003-08-04) [i586-linux]
> > > > > 
> > > > > The error occures after a lot of calls; it reminds me the  
> > > > > behaviour of a stack overflow, or something.
> > > > 
> > > > Show us whole program, if possible, to reproduce your error.
> > > 
> > > I will try, but it is a little bit hard because the C func is called  
> > > from a C++ method deeply inside a complex inheritance tree.
> > > 
> > > Things that I can verify now are:
> > > 
> > > (a) The C func is called with no problem 8000 times.  
> > > (b) The C func is called for 3000+ and segfaults if I put a  
> > > simple rb\_warn() inside. Ruby 1.8.0  
> > > (c) The C func is called for 6000+ and segfaults if I put a  
> > > simple rb\_warn() inside. Ruby 1.8.1 preview 3.
> > 
> > Okay, it is my fault. ☹
> > 
> > While writting a C++ extension for Ruby, I do (almost everywhere):
> > 
> > static VALUE foo\_init (…)  
> > {  
> > Foo \*f = new Foo();  
> > rb\_iv\_set (self, “ **ptr** ”, (VALUE) f);  
> > return self;  
> > }
> > 
> > static VALUE foo\_bar (…)  
> > {  
> > Foo \*f = (Foo \*) rb\_iv\_get (self, “ **ptr** ”);  
> > f-\>Bar();  
> > return self;  
> > }
> > 
> > The error ruby reports is on an address of a Foo created pointer.  
> > If I remove the rb\_iv\_set() call, ruby executes correctly. But, I  
> > have to keep the pointer somewhere in order to execute methods of  
> > the specific class.
> > 
> > Any ideas?
> > 
> > ## Regards,
> > 
> > University of Athens I bet the human brain  
> > Physics Department is a kludge --Marvin Minsky
> 
> Derek Lewis
> 
> ===================================================================  
> Java Web-Application Developer
> 
> ```
> Email : email@lewisd.com
> Cellular : 604.312.2846
> Website : http://www.lewisd.com
> 
> ```
> 
> “If you’ve got a 5000-line JSP page that has “all in one” support  
> for three input forms and four follow-up screens, all controlled  
> by “if” statements in scriptlets, well … please don’t show it  
> to me :-). Its almost dinner time, and I don’t want to lose my  
> appetite :-).”  
> - Craig R. McClanahan

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard2](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Ara.T.Howard2](https://rubytalk.org/u/Ara.T.Howard2)\
**Post date:** [13 December 2003 20:26 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/7 "2003-12-13T20:26:59Z")

</div>

create a struct to wrap Foo objects with and let ruby handle the object:

struct FooWrap { Foo \*foo; };  
…  
…  
VALUE rb\_cFooWrap;  
…  
…  
static VALUE  
FooWrap\_free (FooWrap)  
struct FooWrap _FooWrap;  
{  
delete FooWrap-\>foo;  
free(FooWrap);  
}  
…  
…  
/_ defined this as the class ‘new’ method in Init \*/  
static VALUE  
rb\_cFooWrap\_c\_new (klass)  
VALUE klass;  
{  
struct FooWrap \*FooWrap;  
VALUE obj;  
obj = Data\_Make\_Struct(klass, struct FooWrap, 0, FooWrap\_free, FooWrap);  
return obj;  
}  
…  
…  
static VALUE foo\_init (…)  
{  
rb\_iv\_set (self, “foowrap”, rb\_cFooWrap\_c\_new);  
return self;  
}

static VALUE foo\_bar (…)  
{  
VALUE foowrap;  
struct FooWrap \*FooWrap;

```
  foowrap = rb_iv_get (self, "foowrap");
  Data_Get_Struct(foowrap, struct FooWrap, FooWrap)
  FooWrap->foo->Bar();
  return self;

```

}

i don’t know if you can wrap c++ class directly since Data\_Make\_Struct uses  
ALLOC internally…

-a

> **···**
>
> On Sun, 14 Dec 2003, Elias Athanasopoulos wrote:
> 
> > Date: Sun, 14 Dec 2003 02:33:04 +0900  
> > From: Elias Athanasopoulos [elathan@phys.uoa.gr](mailto:elathan@phys.uoa.gr)  
> > Newsgroups: comp.lang.ruby  
> > Subject: Re: strange rb\_gc\_mark error
> > 
> > On Sun, Dec 14, 2003 at 12:02:27AM +0900, Elias Athanasopoulos wrote:
> > 
> > > On Sat, Dec 13, 2003 at 10:11:12PM +0900, Yukihiro Matsumoto wrote:
> > > 
> > > > Hi,
> > > > 
> > > > In message “strange rb\_gc\_mark error” \> \> \> on 03/12/13, Elias Athanasopoulos [elathan@phys.uoa.gr](mailto:elathan@phys.uoa.gr) writes:
> > > > 
> > > > > I have a pure C function, which may be called arbitrary  
> > > > > sequential times. If I call any rb\_\* function inside, including  
> > > > > the simple rb\_warn(), I get:
> > > > > 
> > > > > BUG] rb\_gc\_mark(): unknown data type 0x28(0x89965b0) non object  
> > > > > ruby 1.8.0 (2003-08-04) [i586-linux]
> > > > > 
> > > > > The error occures after a lot of calls; it reminds me the  
> > > > > behaviour of a stack overflow, or something.
> > > > 
> > > > Show us whole program, if possible, to reproduce your error.
> > > 
> > > I will try, but it is a little bit hard because the C func is called  
> > > from a C++ method deeply inside a complex inheritance tree.
> > > 
> > > Things that I can verify now are:
> > > 
> > > (a) The C func is called with no problem 8000 times.  
> > > (b) The C func is called for 3000+ and segfaults if I put a  
> > > simple rb\_warn() inside. Ruby 1.8.0  
> > > (c) The C func is called for 6000+ and segfaults if I put a  
> > > simple rb\_warn() inside. Ruby 1.8.1 preview 3.
> > 
> > Okay, it is my fault. ☹
> > 
> > While writting a C++ extension for Ruby, I do (almost everywhere):
> > 
> > static VALUE foo\_init (…)  
> > {  
> > Foo \*f = new Foo();  
> > rb\_iv\_set (self, “ **ptr** ”, (VALUE) f);  
> > return self;  
> > }
> > 
> > static VALUE foo\_bar (…)  
> > {  
> > Foo \*f = (Foo \*) rb\_iv\_get (self, “ **ptr** ”);  
> > f-\>Bar();  
> > return self;  
> > }
> > 
> > The error ruby reports is on an address of a Foo created pointer.  
> > If I remove the rb\_iv\_set() call, ruby executes correctly. But, I  
> > have to keep the pointer somewhere in order to execute methods of  
> > the specific class.
> > 
> > Any ideas?
> 
> –
> 
> ATTN: please update your address books with address below!
> 
> ===============================================================================
> 
> > EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov  
> > PHONE :: 303.497.6469  
> > ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328  
> > STP :: [Solar-Terrestrial Physics Data | NCEI](http://www.ngdc.noaa.gov/stp/)  
> > NGDC :: [http://www.ngdc.noaa.gov/](http://www.ngdc.noaa.gov/)  
> > NESDIS :: [http://www.nesdis.noaa.gov/](http://www.nesdis.noaa.gov/)  
> > NOAA :: [http://www.noaa.gov/](http://www.noaa.gov/)  
> > US DOC :: [http://www.commerce.gov/](http://www.commerce.gov/)
> > 
> > The difference between art and science is that science is what we  
> > understand well enough to explain to a computer.  
> > Art is everything else.  
> > – Donald Knuth, “Discover”
> > 
> > /bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’  
> > ===============================================================================

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [13 December 2003 18:28 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/8 "2003-12-13T18:28:13Z")

</div>

> osd = xosd\_create(lines);  
> r\_osd = INT2FIX( (int)osd );

> rb\_iv\_set(self, "@osd", r\_osd);

How do you free the object ?

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Derek\_Lewis](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@Derek\_Lewis](https://rubytalk.org/u/Derek_Lewis)\
**Post date:** [13 December 2003 19:55 UTC](https://rubytalk.org/t/strange-rb-gc-mark-error/9318/9 "2003-12-13T19:55:31Z")

</div>

Well, the osd object has a destroy function:

xosd\_destroy(osd);

which I call from the finalizer of my ruby object.

If you mean the r\_osd object, the FixNum, well, I don’t free it…  
this is my first extension, and I haven’t been able to find any  
documentation on writing ruby extensions outside of pickaxe.

> **···**
>
> On Sun, 14 Dec 2003, ts wrote:
> 
> > > osd = xosd\_create(lines);  
> > > r\_osd = INT2FIX( (int)osd );
> > 
> > > rb\_iv\_set(self, “@osd”, r\_osd);
> > 
> > How do you free the object ?
> > 
> > Guy Decoux
> 
> Derek Lewis
> 
> ===================================================================  
> Java Web-Application Developer
> 
> ```
> Email : email@lewisd.com
> Cellular : 604.312.2846
> Website : http://www.lewisd.com
> 
> ```
> 
> “If you’ve got a 5000-line JSP page that has “all in one” support  
> for three input forms and four follow-up screens, all controlled  
> by “if” statements in scriptlets, well … please don’t show it  
> to me :-). Its almost dinner time, and I don’t want to lose my  
> appetite :-).”  
> - Craig R. McClanahan
