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

  • Thread starter Asbjørn Reglund Thorsen
  • Start date
A

Asbjørn Reglund Thorsen

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
 
T

ts

"A" == =?ISO-8859-1?Q?Asbj=F8rn Reglund Thorsen?= <ISO-8859-1> writes:

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

A> 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.

A> #0 0x00000089 in ?? ()
A> #1 0x40388c19 in free_MenuSystem () from ./mHeat1.so
A> #2 0x0806f636 in rb_gc_call_finalizer_at_exit () at gc.c:1858
A> #3 0x08053e34 in ruby_finalize_1 () at eval.c:1418
A> #4 0x08053f43 in ruby_cleanup (ex=0) at eval.c:1453
A> #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
 
A

Asbjørn Reglund Thorsen

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... :)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top