# Segmentation fault while using Ruby::DL

**URL:** <https://www.ruby-forum.com/t/segmentation-fault-while-using-ruby-dl/188565>\
**Category:** Ruby\
**Created:** [May 13, 2010, 11:24pm UTC](https://www.ruby-forum.com/t/segmentation-fault-while-using-ruby-dl/188565 "2010-05-13T23:24:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [May 13, 2010, 11:24pm UTC](https://www.ruby-forum.com/t/segmentation-fault-while-using-ruby-dl/188565/1 "2010-05-13T23:24:07Z")

</div>

Hello.

I tried to call a simple method on Windows (XP, Win7 - didn’t matter)  
machine and it seems that i will get a segmentation fault depending of  
the Ruby version. Any ideas why that might happen?

Here is the offending code (doc for EnumWindows is at  
[EnumWindows function (winuser.h) - Win32 apps | Microsoft Learn](http://msdn.microsoft.com/en-us/library/ms633497(v=VS.85).aspx)):

require ‘dl/import’

module Testing

extend DL::Importable

dlload “user32.dll”

USER32 = DL.dlopen(“user32”)  
EnumWindows = USER32[‘EnumWindows’, ‘IPL’]

CALLBACK = DL.callback(‘ILL’) do |curr\_hwnd, p|  
p curr\_hwnd  
0  
end

module\_function

def enumerate  
EnumWindows.call(CALLBACK, 0)  
end  
end

Testing.enumerate

* * *

And when trying to run it with different Ruby 1.8.6 patchlevels, i see  
the following output:

S:\>ruby -v  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

S:\>testing.rb  
328312

S:\>ruby -v  
ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32]

S:\>testing.rb  
393884  
S:/testing.rb:20: [BUG] Segmentation fault  
ruby 1.8.6 (2010-02-04) [i386-mingw32]

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

* * *

So, p398 gives me a segfault. On the other hand FFI example at  
[http://wiki.github.com/ffi/ffi/windows-examples](http://wiki.github.com/ffi/ffi/windows-examples) works perfectly.

Does that mean that I have to convert one old Ruby lib to use FFI  
altogether? Or even if it doesn’t mean that, then would you recommend me  
to do it?

Jarmo

---

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [May 14, 2010, 11:13am UTC](https://www.ruby-forum.com/t/segmentation-fault-while-using-ruby-dl/188565/2 "2010-05-14T11:13:35Z")

</div>

Just wanted to let you know that segfault doesn’t happen with win32-api  
also.

Jarmo

---

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [May 15, 2010, 12:12am UTC](https://www.ruby-forum.com/t/segmentation-fault-while-using-ruby-dl/188565/3 "2010-05-15T00:12:14Z")

</div>

> So, p398 gives me a segfault. On the other hand FFI example at  
> [http://wiki.github.com/ffi/ffi/windows-examples](http://wiki.github.com/ffi/ffi/windows-examples) works perfectly.

It’s probably a bug. You can report it at [redmine.ruby-lang.org](http://redmine.ruby-lang.org)

> Does that mean that I have to convert one old Ruby lib to use FFI  
> altogether? Or even if it doesn’t mean that, then would you recommend me  
> to do it?

I’d recommend using FFI only. Plus you get cross VM coolness points.  
-rp

---

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [May 15, 2010, 1:07am UTC](https://www.ruby-forum.com/t/segmentation-fault-while-using-ruby-dl/188565/4 "2010-05-15T01:07:04Z")

</div>

Roger P. wrote:

> > So, p398 gives me a segfault. On the other hand FFI example at  
> > [http://wiki.github.com/ffi/ffi/windows-examples](http://wiki.github.com/ffi/ffi/windows-examples) works perfectly.
> 
> It’s probably a bug. You can report it at [redmine.ruby-lang.org](http://redmine.ruby-lang.org)
> 
> > Does that mean that I have to convert one old Ruby lib to use FFI  
> > altogether? Or even if it doesn’t mean that, then would you recommend me  
> > to do it?
> 
> I’d recommend using FFI only. Plus you get cross VM coolness points.  
> -rp

Does your recommendation also apply when the library in question is  
Windows only?

Jarmo

---

<div class="post-metadata">

**Author:** ![juuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@juuser](https://www.ruby-forum.com/u/juuser)\
**Post date:** [May 15, 2010, 1:39am UTC](https://www.ruby-forum.com/t/segmentation-fault-while-using-ruby-dl/188565/5 "2010-05-15T01:39:37Z")

</div>

> > I’d recommend using FFI only. Plus you get cross VM coolness points.  
> > -rp
> 
> Does your recommendation also apply when the library in question is  
> Windows only?

I’m not actually totally into the subject (I’ve done a few ffi  
examples), but I’ve heard that ffi is cleaner than DL, so…a weak  
recommendation for “always” from me, it appears.  
-rp
