# Valgrind and embedded ruby

**URL:** <https://www.ruby-forum.com/t/valgrind-and-embedded-ruby/48486>\
**Category:** Ruby\
**Created:** [November 21, 2005, 9:23am UTC](https://www.ruby-forum.com/t/valgrind-and-embedded-ruby/48486 "2005-11-21T09:23:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ihspm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8e8cbc/32.png) [@ihspm](https://www.ruby-forum.com/u/ihspm)\
**Post date:** [November 21, 2005, 9:23am UTC](https://www.ruby-forum.com/t/valgrind-and-embedded-ruby/48486/1 "2005-11-21T09:23:41Z")

</div>

Hi all,

I’m currently testing an environment with embedding ruby and found an  
issue when allocating a large amount of ruby string objects in external  
C calls. Im not sure, whether I misunderstood the correct usage.

The Ruby version used is 1.8.4, own compile on red hat EL update 5.

## The C environment is:

#include “ruby.h”

int main (int argc, char \*argv[])  
{  
VALUE cModule;  
VALUE message;

```
ruby_init();
cModule = rb_define_module("CModule");

rb_require("MemCheck.rb");

for (long long l=0; l<1000LL; l++) {
// convert C string to Ruby object
message = rb_str_new2("C says Hello to Ruby."
		      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                          "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                          "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

rb_funcall(cModule, rb_intern("sendToRuby"), 1, message);
}

return 0;

```

## }

## The Ruby script:

print(“This is Ruby, required MemCheck.rb\n”);

## module CModule def CModule.sendToRuby(msgFromC) end end

If I use valgrind to check for memory leaks, all is OK. If I now change  
the loop count from 1000LL to 10000LL, valgrind throws errors:

==7903== Conditional jump or move depends on uninitialised value(s)  
==7903== at 0x806C904: gc\_mark\_children (gc.c:758)  
==7903== by 0x806C88F: gc\_mark (gc.c:731)  
==7903== by 0x806C6C2: mark\_locations\_array (gc.c:626)  
==7903== by 0x806C6EF: rb\_gc\_mark\_locations (gc.c:639)

(in total 496 errors from 39 contexts)

Has anybody seen this behavior? Where am I wrong?

Any help is appreciated, best regards

Heiko

---

<div class="post-metadata">

**Author:** ![ihspm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8e8cbc/32.png) [@ihspm](https://www.ruby-forum.com/u/ihspm)\
**Post date:** [November 21, 2005, 6:21pm UTC](https://www.ruby-forum.com/t/valgrind-and-embedded-ruby/48486/2 "2005-11-21T18:21:18Z")

</div>

Quoting Heiko L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> (in total 496 errors from 39 contexts)

This is probably the consequence of the “conservative” portion of  
Ruby’s garbage collector, which scans the stack and some other  
areas which could be uninitialized.

You’ll see similar warnings from valgrind in the presence of other  
conservative GCs (for example, the boehm collector for C).

This is a consequence of the fact that a conservative collector must  
scan memory without knowing whether it is initialized/valid in  
advance or not. Conservative collectors are designed to be robust  
in the face of garbage pointers, however, so this is perfectly  
safe.

-mental

---

<div class="post-metadata">

**Author:** ![ihspm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8e8cbc/32.png) [@ihspm](https://www.ruby-forum.com/u/ihspm)\
**Post date:** [November 21, 2005, 6:53pm UTC](https://www.ruby-forum.com/t/valgrind-and-embedded-ruby/48486/3 "2005-11-21T18:53:33Z")

</div>

That’s great information, thanks.

I was just surprised, that the smaller loop doesn’t throw any errors  
and as soon as the allocated string objects exceed a given memeory  
size, something seems to go wrong. Since Ruby is widely used, that  
hinted at an usage error.

Thanks again and best regards,

Heiko

---

<div class="post-metadata">

**Author:** ![ihspm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8e8cbc/32.png) [@ihspm](https://www.ruby-forum.com/u/ihspm)\
**Post date:** [November 21, 2005, 8:54pm UTC](https://www.ruby-forum.com/t/valgrind-and-embedded-ruby/48486/4 "2005-11-21T20:54:34Z")

</div>

Quoting Heiko L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I was just surprised, that the smaller loop doesn’t throw any  
> errors and as soon as the allocated string objects exceed a given  
> memeory size, something seems to go wrong.

There’s a bit of a hysteresis in the garbage collection; probably  
there aren’t any collection passes being triggered until the  
allocated space passes a certain threshold.

-mental
