12

I've compiled ruby (2.2.4) with debugflags='-g' optflags='-O0' ./configure --with-valgrind as suggested by the valgrind-docs. According to at least several though obscure resources people have used Valgrind with ruby successfully, more so the --with-valgrind option appears to exist as of 1.9.3

./doc/ChangeLog-1.9.3:62800: * configure.in: add --with-valgrind.

The error I have is identical to the one referred to in the analogous python question, where even a basic test program reports numerous false positives for 'reference lost' / 'probably lost'.

sample output:

valgrind --tool=memcheck --leak-check=yes --max-stackframe=8382448 --track-origins=yes ruby test.rb

==15105== HEAP SUMMARY:
==15105==     in use at exit: 840,786 bytes in 8,050 blocks
==15105==   total heap usage: 10,496 allocs, 2,446 frees, 2,373,732 bytes allocated
==15105== 
==15105== 16 bytes in 1 blocks are possibly lost in loss record 87 of 5,245
==15105==    at 0x4C2ABD0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15105==    by 0x1511F4: objspace_xmalloc0 (gc.c:7780)
==15105==    by 0x1512CB: objspace_xmalloc (gc.c:7792)
==15105==    by 0x15153F: ruby_xmalloc (gc.c:7871)
==15105==    by 0x2E54CA: rb_class_subclass_add (class.c:41)
==15105==    by 0x2E546F: RCLASS_SET_SUPER (internal.h:602)
==15105==    by 0x2E58E8: rb_class_boot (class.c:205)
==15105==    by 0x2E687B: boot_defclass (class.c:537)
==15105==    by 0x2E6937: Init_class_hierarchy (class.c:554)
==15105==    by 0x193EEE: InitVM_Object (object.c:3371)
==15105==    by 0x1951C8: Init_Object (object.c:3596)
==15105==    by 0x15C07F: rb_call_inits (inits.c:23)
.....
Community
  • 1
  • 1
user3467349
  • 3,043
  • 4
  • 34
  • 61
  • 2
    Memory allocated during initialization lasts until Ruby terminates, so it does not attempt to free it, leaving that to the kernel. While innocuous, this generates lots of false positives in debuggers. You may want to suppress them: http://valgrind.org/docs/manual/manual-core.html#manual-core.suppress –  May 07 '16 at 13:06
  • @Rei I've tried using `--gen-suppressions` but it's very hard to get everything covered using that, because a different bit of code will generate other false positives. There may be a correct way to genericize the suppressions generated I've found https://wiki.wxwidgets.org/Valgrind_Suppression_File_Howto as an example but it's quite difficult to get effective suppression coverage to say the least. Is there a compile option or a patch I may be missing that would make Ruby's memory handling better compatible with valgrind memory allocation tracking? – user3467349 May 07 '16 at 13:42
  • From my experience working with Ruby's C API, I suspect there's a lot that it doesn't properly clean up. For example, [you cannot create multiple Ruby VMs in one process](http://silverhammermba.github.io/emberb/embed/#limitations). To me that's a strong indication that it relies on the kernel to fully deallocate everything. – Max Nov 16 '22 at 14:16

1 Answers1

1

I would recommend using ruby_memcheck

https://github.com/Shopify/ruby_memcheck

This gem provides a sane way to use Valgrind's memcheck on your native extension gem.

Dorian
  • 7,749
  • 4
  • 38
  • 57