1.8.0 pb

T

thierry wilmot

hello,
I have experiencing a quite strange thing when I tried to
convert my ruby embedded prog from 1.6.8 to 1.8.0

in 1.6.8, all my structs/classes where wrapped this way:

rb_define_method(rb_foo, "create", (RUBY_FUN_REF)foo_create, 0);

static VALUE foo_create(VALUE self)
{
foo *ptr = new foo(self);
rb_iv_set(self, "ruby2cpp__", INT2FIX(ptr));
VALUE obj = Data_Wrap_Struct(self, 0, cb_destroy_foo, ptr);
}

when I link my prog with the new 1.8.0 lib, cb_destroy_foo()
is called even when the Ruby wrapped object is still valid !!!!
(too many changes in gc.c from 1.6.8...)

after many seek, my code should be this way to works fine in 1.8.0 :

// add two methods

rb_define_singleton_method(rb_foo, "new", (RUBY_FUN_REF)foo_new, 0);
rb_define_method(rb_foo, "initialize", (RUBY_FUN_REF)foo_initialize, 1);

static VALUE foo_new(VALUE self)
{
VALUE argv[1];

foo *ptr = new foo(self);
VALUE obj = Data_Wrap_Struct(self, 0, cb_destroy_foo, ptr);

argv[0] = INT2FIX(ptr);
rb_obj_call_init(obj, 1, argv);

return obj;
}

static VALUE foo_initialize(VALUE self, VALUE init)
{
rb_iv_set(self, "ruby2cpp__", init);

return self;
}

why should I 'overload' new and initialize in 1.8.0, and expand
my 3 code lignes to so much lines !!


thanks,
Thierry
 
Y

Yukihiro Matsumoto

Hi,

In message "1.8.0 pb"

|I have experiencing a quite strange thing when I tried to
|convert my ruby embedded prog from 1.6.8 to 1.8.0

Hmm.

|why should I 'overload' new and initialize in 1.8.0, and expand
|my 3 code lignes to so much lines !!

There's no reason for it. Show me the code if possible.

matz.
 
N

nobu.nokada

Hi,

At Tue, 26 Aug 2003 04:45:25 +0900,
thierry said:
in 1.6.8, all my structs/classes where wrapped this way:

rb_define_method(rb_foo, "create", (RUBY_FUN_REF)foo_create, 0);

static VALUE foo_create(VALUE self)
{
foo *ptr = new foo(self);
rb_iv_set(self, "ruby2cpp__", INT2FIX(ptr));
VALUE obj = Data_Wrap_Struct(self, 0, cb_destroy_foo, ptr);
}

I can't see the intention of the code. Is rb_foo a subclass of
Class? Otherwise, you must not pass self to Data_Wrap_Struct().
// add two methods

rb_define_singleton_method(rb_foo, "new", (RUBY_FUN_REF)foo_new, 0);

You should use rb_define_alloc_func() instead of overriding
"new".

static VALUE foo_allocate(VALUE self)
{
foo *ptr = new foo(self);
VALUE obj = Data_Wrap_Struct(self, 0, cb_destroy_foo, ptr);
rb_iv_set(obj, "ruby2cpp__", INT2FIX(ptr));
return obj;
}
 
T

thierry wilmot

Matz, I can give you my wrapper code but I'm afraid it's too big (116
Ko)... :)

In fact, after seeing the output result of swig wrapper, I decide not to
override the new and initialize ruby function (as written in the pickaxe
too) and just called Data_Wrap_Struct() after all ruby init of an
instance, in a foo_create() function. The main constraint of that is :
the cpp part of the object ( my cpp class) is not newed before the call
of the ruby function create()... this work fine from 1.6.6 to 1.6.8 version.

in 1.8.0, it's seems that if I not put the Data_Wrap_Struct() before the
initialize() function call, the gc from the gc_sweep() function line
939, call obj_free() that call my cpp destructor function
cb_destroy_foo() on some still alive objects . ?! Internal flags on
these objects (as.basic.flags) are FL_MARK less.


I will try to extract this problem in a small sample , but it's not
obvious. Anyway putting Data_Wrap_Struct() in the foo_new() seems to
work quite fine.
I can't see the intention of the code. Is rb_foo a subclass of
Class? Otherwise, you must not pass self to Data_Wrap_Struct().

rb_foo = rb_define_class("Foo", rb_cObject);

the first arg of Data_Wrap_Struct() should be the class ref ? no ?
 
N

nobu.nokada

Hi,

At Wed, 27 Aug 2003 04:48:35 +0900,
thierry said:
rb_foo = rb_define_class("Foo", rb_cObject);

the first arg of Data_Wrap_Struct() should be the class ref ? no ?

Yes. But you define a instance method "create" instead of a
class method. So self in foo_create() is an instance of Foo.
This certainly causes GC crashed.
 
A

aa

Hi,

yes, you are right... passing a self instance instead of class
cause GC calling some alive functions (not crashed)

thank you nobu
 
N

nobu.nokada

Hi,

At Thu, 28 Aug 2003 04:52:14 +0900,
aa said:
yes, you are right... passing a self instance instead of class
cause GC calling some alive functions (not crashed)

It caused crash in fact, so now it is checked in 1.8 CVS head.

| Thu Aug 14 11:27:37 2003 NAKAMURA Usaku <[email protected]>
|
| * gc.c (rb_data_object_alloc): check type of 1st argument.
| [ruby-dev:21192]

Your code in [ruby-talk:80174] will occur a runtime error.
 

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,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top