views:

264

answers:

2

A follow up to an earlier question, showing the part that fails when I try to get the error message from my target library:

require 'gt4r'

@@test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini"
@@normal_user = "BMCHARGUE"

describe Gt4r do
  it 'initializes' do
      rv = Gt4r.gTD_initialize @@normal_user, @@normal_user, @@test_environment
      Gt4r.gTD_get_error_message rv, @msg
      @msg.should == ""
      rv.should == 0
  end
end

I expect the error message to be returned in @msg, but when run I get the following:

Gt4r
(eval):5: [BUG] Segmentation fault
ruby 1.8.6 (2008-08-11) [i386-mswin32]


This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

And this if I use a symbol (:msg) instead:

C:\code\GraphTalk\gt4r_dl>spec -fs -rgt4r gt4r_spec.rb

Gt4r
- initializes (ERROR - 1)

1)
NoMethodError in 'Gt4r initializes'
undefined method `to_ptr' for :msg:Symbol
(eval):5:in `call'
(eval):5:in `gTD_get_error_message'
./gt4r_spec.rb:9:

Finished in 0.046 seconds

1 example, 1 failure

Clearly I am missing something about passing parameters between ruby and C. What kind of ruby variable do I need to get my value returned?

+1  A: 

Which bit fails? I'm guessing it's this:

Gt4r.gTD_get_error_message rv, @msg

How is the method implemented? What's the C signature of the method? (*char)?

Btw: at the risk of sounding like broken record, it's MUCH easier to write a C extension than trying to shoehorn things into DL. AFAIK, no one uses DL, apart from maybe one-off hacks to map single functions. You mentioned FFI doesn't work under Windows, you may want to have a look at RubyInline (http://www.zenspider.com/ZSS/Products/RubyInline/), or just use a plain C extension (if you know C well enough to use DL, you know C well enough to write an extension)

A: 

Is @msg even initialized at this point:

Gt4r.gTD_get_error_message rv, @msg

If @msg is nil, DL will probably cast it to a null pointer which crashes when your lib tries to dereference it.