FFI::Struct and FFI::Union do not call member struct's initialize method
------------------------------------------------------------------------

                 Key: JRUBY-4139
                 URL: http://jira.codehaus.org/browse/JRUBY-4139
             Project: JRuby
          Issue Type: Bug
          Components: Extensions
    Affects Versions: JRuby 1.4.0RC2, JRuby 1.3.1
            Reporter: John Croisant
            Priority: Minor


If you define an FFI::Struct-based or FFI::Union-based class with a 
FFI::Struct-based class (let's call it MyStruct) as a member in the 
struct/union, then use the #[] instance method to access the member, it returns 
an instance of MyStruct. This is good. 

But with JRuby, MyStruct#initialize is never actually called for the new 
MyClass instance when it is created. This is bad. In my case, the #initialize 
method was supposed to initialize some instance variables. But it was never 
called, so the variables were never initialized, which led to mysterious 
breakage elsewhere in the program.

Code says it most clearly:

{noformat}
require 'ffi'

class MyStruct < FFI::Struct
  layout :a, :int

  def initialize( *args )
    puts "Called MyStruct#initialize for #{self.inspect}"
    super
  end
end


class OtherStruct < FFI::Struct
  layout :mystruct, MyStruct
end

ostruct = OtherStruct.new( FFI::MemoryPointer.new(:int) )

puts "Accessing ostruct[:mystruct]..."
p ostruct[:mystruct]


class MyUnion < FFI::Union
  layout :mystruct, MyStruct
end

myunion = MyUnion.new( FFI::MemoryPointer.new(:int) )

puts "Accessing myunion[:mystruct]..."
p myunion[:mystruct]
{noformat}

* Expected: When run, the script should print:
{noformat}
Accessing ostruct[:mystruct]...
Called MyStruct#initialize for #<MyStruct:0xb7728488>
#<MyStruct:0xb7728488>
Accessing myunion[:mystruct]...
Called MyStruct#initialize for #<MyStruct:0xb77281e0>
#<MyStruct:0xb77281e0>
{noformat}

* Observed: When run with JRuby 1.4.0 RC2 (and 1.3.1, and perhaps earlier), the 
script prints only:
{noformat}
Accessing ostruct[:mystruct]...
#<MyStruct:0x1f2f70a>
Accessing myunion[:mystruct]...
#<MyStruct:0x6bd9e0>
{noformat}

Which indicates that MyStruct#initialize is not being called, despite new 
instances of MyStruct being created.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to