[n00b] Garbage collection, Ruby C++ and SWIG

I`m having trouble with an interface to a c++ library. It seems like Ruby`s GC is collecting objects that it shouldn`t. I have read the SWIG-Ruby documentation on the matter, but I am unsure of where to put the markfunc , and how to get it to interact with my program.

My interface file : mHeat1.i
My Ruby code : run.rb

require 'mHeat1'

menu = MHeat1::MenuSystem.new
menu.init("Ruby Interface", "This is so cool !")
heat = MHeat1::Heat1.new
heat.define(menu)
heat.scan
heat.solveProblem
heat.resultReport

The problem is the MenuSystem, I found out by using gdb:
gdb ruby
(gdb)run run.rb

...

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 16384 (LWP 1345)]
0x00000089 in ?? ()
(gdb) where
#0 0x00000089 in ?? ()
#1 0x40388c19 in free_MenuSystem () from ./mHeat1.so
#2 0x0806f636 in rb_gc_call_finalizer_at_exit () at gc.c:1858
#3 0x08053e34 in ruby_finalize_1 () at eval.c:1418
#4 0x08053f43 in ruby_cleanup (ex=0) at eval.c:1453
#5 0x08054081 in ruby_stop (ex=135573240) at eval.c:1484
#6 0x080540ef in ruby_run () at eval.c:1505
#7 0x08052245 in main (argc=135573240, argv=0x814aef8, envp=0xbffff020)
     at main.c:46

I`m having trouble with an interface to a c++ library. It seems like
Ruby`s GC is collecting objects that it shouldn`t. I have read the
SWIG-Ruby documentation on the matter, but I am unsure of where to put the
markfunc , and how to get it to interact with my program.

My interface file : mHeat1.i

Well, difficult to say without seeing the source but probably the problem
has nothing to do with the mark function.

#0 0x00000089 in ?? ()
#1 0x40388c19 in free_MenuSystem () from ./mHeat1.so
#2 0x0806f636 in rb_gc_call_finalizer_at_exit () at gc.c:1858
#3 0x08053e34 in ruby_finalize_1 () at eval.c:1418
#4 0x08053f43 in ruby_cleanup (ex=0) at eval.c:1453
#5 0x08054081 in ruby_stop (ex=135573240) at eval.c:1484

ruby is at the end (it will exist) and it call the finalizers : this is the
normal operation.

There are many reasons for such a crash :
   * free_MenuSystem() is called twice
   * the free function must be called in a predefined order
   * ...

Difficult to say more without the source.

Guy Decoux

The header file is located at http://folk.uio.no/asbjoert/MenuSystem.h

···

On Sat, 15 Jan 2005, ts wrote:

> I`m having trouble with an interface to a c++ library. It seems like
> Ruby`s GC is collecting objects that it shouldn`t. I have read the
> SWIG-Ruby documentation on the matter, but I am unsure of where to put the
> markfunc , and how to get it to interact with my program.

> My interface file : mHeat1.i

Well, difficult to say without seeing the source but probably the problem
has nothing to do with the mark function.

> #0 0x00000089 in ?? ()
> #1 0x40388c19 in free_MenuSystem () from ./mHeat1.so
> #2 0x0806f636 in rb_gc_call_finalizer_at_exit () at gc.c:1858
> #3 0x08053e34 in ruby_finalize_1 () at eval.c:1418
> #4 0x08053f43 in ruby_cleanup (ex=0) at eval.c:1453
> #5 0x08054081 in ruby_stop (ex=135573240) at eval.c:1484

ruby is at the end (it will exist) and it call the finalizers : this is the
normal operation.

There are many reasons for such a crash :
  * free_MenuSystem() is called twice
  * the free function must be called in a predefined order
  * ...

Difficult to say more without the source.

Guy Decoux

The header file is located at http://folk.uio.no/asbjoert/MenuSystem.h

Do diffpack (it's diffpack no ?), or one of the libraries (tk, mesa, ...)
is compiled with pthread support ?

Guy Decoux

I just now, 5 minutes ago, solved the problem:
It was the diffpack program`s job to erase it`s object. I just had to make Ruby know it, so I used the "freefunc" (ref SWIG-doc) function and it magically worked. =)
I am really beginning to enjoy Ruby/SWIG !! The SWIG-doc was a little misleading, because it doesn`t tell that the %freefunc directive must be declared _before_ the class it`s "freeing" in the interface file.
But thank you anyways for trying to help me... :slight_smile:

Best regards from Norway....Yes, Ruby is getting quite popular here :slight_smile:

···

On Mon, 17 Jan 2005, ts wrote:

> The header file is located at http://folk.uio.no/asbjoert/MenuSystem.h

Do diffpack (it's diffpack no ?), or one of the libraries (tk, mesa, ...)
is compiled with pthread support ?

Guy Decoux