From: Craig Files Date: 2001-08-02T06:18:53+09:00 Subject: [ruby-talk:18994] Re: Possible SWIG Ruby bug Craig Files wrote: After get no response, I thought that I would try to determine if this was a SWIG or a Ruby bug. It is a SWIG bug, in that SWIG does not differentiate between the pointer returned from a constructor or a method. If anybody is interested, I can email the changes to the SWIG ruby.cxx file that I have made.... I have also posted this bug on SourceForge, but my solution was not accepted by the bug tracker. > Hi, > > It seems that when using SWIG with Ruby, there is a bug that causes a destructor for an object to be called more than once. If an object is newed > > and deleted within C++, but a pointer to that object is returned to Ruby during execution of a Ruby program, the object is deleted twice (once in > > C++ and once in Ruby). > > The following code recreates the bug. The resulting core file shows the point of failure in the destructor for class Bar. > > Has anyone else encountered this bug when using SWIG? Is there a way around this problem? That is, we would like the ability > > to delete a object Bar if we create it in Ruby (i.e. bar = Bar.new()). > > ----------------------------------------- Foo.h > > class Bar { > public: > int * data; > Bar() : data (new int) {} > ~Bar() { delete data; } > }; > > class Foo { > private: > Bar *V; > public: > Foo() : V(new Bar) {} > ~Foo() { delete V; } > const Bar *GetBar() { return V; } > }; > > ----------------------------------------- Foo.i > > %module Foo > %{ > #include "Foo.h" > %} > > class Bar { > public : > Bar(); > ~Bar(); > }; > > class Foo { > public: > Foo(); > ~Foo(); > const Bar *GetBar(); > } > > ------------------------------------------- foo.rb > > require 'Foo' > > f = Foo::Foo.new() > b = f.GetBar() > puts "exiting" > > -------------------------------------------- output > > exiting > test.rb:5: [BUG] Segmentation fault > ruby 1.6.4 (2001-06-04) [i686-linux] > Aborted > > Thanks, > > Craig > > -- > ___________________________________________________________________________ > Craig Files | / > http://fccf0183l.ftc.agilent.com/~cfiles/ | __o / > craig_files@agilent.com | \<, / > ____________________________________________|_____()/ ()____/ -- ___________________________________________________________________________ Craig Files | / http://fccf0183l.ftc.agilent.com/~cfiles/ | __o / craig_files@agilent.com | \<, / ____________________________________________|_____()/ ()____/ (970)288-0183