From: Gennady Bystritksy Date: 2005-04-29T08:03:14+09:00 Subject: Cloning SWIG wrapped C++ classes Hi, guys I have a simple C++ class that I want to use in Ruby. I wrapped it with SWIG 1.3.24 as an extension "sample" for Ruby 1.8.2. Everything works fine until I try to clone (or dup) that class' instance in Ruby. The duplicated instance is unusable as there seems to be no underlying C++ instance for the cloned Ruby proxy instance (see below). Do I do something wrong? I would expect my explicitly specified copy constructor to be used in this case. Here's the code and IRB session: ### Greeter.h class Greeter { public: Greeter(); Greeter(const Greeter& other); virtual ~Greeter(); void hello() const; }; ### Greeter.cc Greeter::Greeter() { std::cout << "Greeter::Greeter()" << std::endl; } Greeter::~Greeter() { std::cout << "Greeter::~Greeter()" << std::endl; } Greeter::Greeter(const Greeter& other) { std::cout << "Greeter::Greeter(const Greeter& other)" << std::endl; } void Greeter::hello() const { std::cout << "HELLO !!!" << std::endl; } ### sample.i %module sample %{ #include "Greeter.h" %} %include "Greeter.h" [linux.gfbs:722]gfb_1> /usr/local/ruby-1.8/bin/irb irb(main):001:0> require 'sample' => true irb(main):002:0> g = Sample::Greeter.new Greeter::Greeter() => # irb(main):003:0> g.hello HELLO !!! => nil irb(main):004:0> gg = g.dup => # irb(main):005:0> gg.hello RuntimeError: This Greeter * already released from (irb):5:in `hello' from (irb):5 irb(main):006:0> ggg = g.clone => # irb(main):007:0> ggg.hello RuntimeError: This Greeter * already released from (irb):7:in `hello' from (irb):7 irb(main):008:0> g.hello HELLO !!! => nil irb(main):009:0> Any ideas would be greatly appreciated. Gennady.