Subject:
[ruby-ffi] Structs layout and memory consumption
From:
Hans Hasselberg
Date:
11/8/12 1:45 PM
To:
ruby-ffi@googlegroups.com

Hello!

Thank you very much for this library!
I stumbled over a problem with code I 'inherited', it is a gem which wraps libcurl.
This code works, but consumes too much memory for my taste and looks suspicious!

https://github.com/typhoeus/ethon/blob/master/lib/ethon/curls/classes.rb:

class FDSet < FFI::Struct
  FD_SETSIZE = 524288
  layout :fds_bits, [:long, FD_SETSIZE / FFI::Type::LONG.size]
end

I can change that code to:

class FDSet < FFI::Struct
  layout :fds_bits, :long
end

Instances of FDSet do not consume so much memory any more, but I get segfaults on a regular basis. It seems to me, that fds_bits is just a memory placeholder.
The ffi code in that particular project looks quite understandable except for that FDSet. 

Could you explain to me, why my code segfaults with the changed code and if FDSet is correct specified? I bet you need more information, but I don't know which, so feel free to ask!


Cheers,
Hans