1.8.0 pb

Discussion in 'Ruby' started by thierry wilmot, Aug 25, 2003.

  1. 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
     
    thierry wilmot, Aug 25, 2003
    #1
    1. Advertising

  2. Hi,

    In message "1.8.0 pb"
    on 03/08/26, thierry wilmot <> writes:

    |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.
     
    Yukihiro Matsumoto, Aug 26, 2003
    #2
    1. Advertising

  3. thierry wilmot

    Guest

    Hi,

    At Tue, 26 Aug 2003 04:45:25 +0900,
    thierry wilmot wrote:
    > 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;
    }

    --
    Nobu Nakada
     
    , Aug 26, 2003
    #3
  4. 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 ?
     
    thierry wilmot, Aug 26, 2003
    #4
  5. thierry wilmot

    Guest

    Hi,

    At Wed, 27 Aug 2003 04:48:35 +0900,
    thierry wilmot wrote:
    > >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 ?


    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.

    --
    Nobu Nakada
     
    , Aug 27, 2003
    #5
  6. thierry wilmot

    aa Guest

    Hi,

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

    thank you nobu

    wrote:
    > Hi,
    >
    > At Wed, 27 Aug 2003 04:48:35 +0900,
    > thierry wilmot wrote:
    >
    >> >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 ?

    >
    >
    > 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.
    >
     
    aa, Aug 27, 2003
    #6
  7. thierry wilmot

    Guest

    Hi,

    At Thu, 28 Aug 2003 04:52:14 +0900,
    aa wrote:
    > 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 <>
    |
    | * 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.

    --
    Nobu Nakada
     
    , Aug 28, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page