Ruby C++ extend, prb with free object

T

Tonyryu Tonyryu

Sorry for my bad english.

I try to create an extention of interpreter ruby, with new class and
module. But when i test my code, the object is never destruct before
program's ending. Maybe the garbage collector is disable or other, i
don't know.

My simple code C++ :

------------------------------------------------------------------------
#include <iostream.h>
#include "ruby.h"

// Class C++ Color
class Color
{
private:
int test;

public:
Color();
~Color();
void proc_test();
void init();
};

Color::Color()
{
std::cout << "constructeur\n" ;
}

Color::~Color()
{
std::cout << "destructeur\n" ;
}

void Color::proc_test()
{
std::cout << "test\n" ;
}

void Color::init()
{
std::cout << "init\n" ;
}

/*********************************************/

static void rb_color_free( Color *objColor )
{
std::cout << "rb_color_free: enter\n";
delete objColor;
std::cout << "rb_color_free: leave\n";
}

static VALUE rb_color_new( VALUE self )
{
Color *objColor = new Color();
return Data_Wrap_Struct(self, 0, rb_color_free, objColor);
}

static VALUE rb_color_init( VALUE self )
{
Color *objColor;
Data_Get_Struct(self, Color, objColor);
objColor->init();
return self;
}

static VALUE rb_color_test(VALUE self)
{
Color *objColor;
Data_Get_Struct (self, Color, objColor);
objColor->proc_test();
}

VALUE cColor;

void init_color()
{

cColor = rb_define_class( "Color", rb_cObject );

rb_define_alloc_func(cColor, rb_color_new);
rb_define_method( cColor, "initialize", (VALUE (*)(...)
)rb_color_init, 0 );
rb_define_method( cColor, "essai", (VALUE (*)(...) )rb_color_test, 0
);
}


int main( int argc, char *argv[] ) {

// Init ruby environment
ruby_init();
ruby_script("embedded");
ruby_init_loadpath();

// Load my class Color definition
init_color();

rb_load_file("test.rb");
ruby_run();

return 0;
}
------------------------------------------------------------------------

And the script 'test.rb' :

------------------------------------------------------------------------
def test
maCouleur = Color.new()
maCouleur.essai
end

test()
maCouleur = Color.new()
------------------------------------------------------------------------

result :

------------------------------------------------------------------------
constructeur
init
test
constructeur
init
rb_color_free: enter
destructeur
rb_color_free: leave
rb_color_free: enter
destructeur
rb_color_free: leave
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top