Cloning SWIG wrapped C++ classes

G

Gennady Bystritksy

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()
=> #<Sample::Greeter:0x402deff0>
irb(main):003:0> g.hello
HELLO !!!
=> nil
irb(main):004:0> gg = g.dup
=> #<Sample::Greeter:0x402db918>
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
=> #<Sample::Greeter:0x402d5a7c>
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top