From 6e144603342da3843f7a8591058bd86f73640380 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Wed, 13 Dec 2017 00:21:56 +0100 Subject: [PATCH] Ported enhancements for #33 fix (factory callbacks) to Ruby too. --- src/rba/rba/rbaInternal.cc | 8 ++++++++ testdata/ruby/basic_testcore.rb | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/rba/rba/rbaInternal.cc b/src/rba/rba/rbaInternal.cc index e239f8404..dc6be87cb 100644 --- a/src/rba/rba/rbaInternal.cc +++ b/src/rba/rba/rbaInternal.cc @@ -327,6 +327,14 @@ Proxy::call (int id, gsi::SerialArgs &args, gsi::SerialArgs &ret) const push_arg (meth->ret_type (), ret, rb_ret, heap); + if (meth->ret_type ().pass_obj ()) { + // In factory callbacks, make sure the returned object is not deleted by + // anyone except the caller. + Proxy *p = 0; + Data_Get_Struct (rb_ret, Proxy, p); + p->keep (); + } + // a Ruby callback must not leave temporary objects tl_assert (heap.empty ()); diff --git a/testdata/ruby/basic_testcore.rb b/testdata/ruby/basic_testcore.rb index 2b6477ee6..d9a2c0e85 100644 --- a/testdata/ruby/basic_testcore.rb +++ b/testdata/ruby/basic_testcore.rb @@ -53,6 +53,27 @@ class RBA::E @m = nil end +class RBAGObject < RBA::GObject + def initialize(z) + super() + @z = z + end + # reimplementation of "virtual int g()" + def g + return @z*2 + end +end + +class RBAGFactory < RBA::GFactory + def initialize + super() + end + # reimplementation of "virtual GObject *f(int)" + def f(z) + return RBAGObject::new(z) + end +end + class Basic_TestClass < TestBase def test_FIRST @@ -2633,4 +2654,19 @@ class Basic_TestClass < TestBase end + # Custom factory implemented in Ruby + def test_80 + + gc = RBA::GObject.g_inst_count + gf = RBAGFactory::new + go = RBA::GFactory.create_f(gf, 17) + assert_equal(go.g_virtual, 34) + assert_equal(go.g_org, 0) + assert_equal(RBA::GObject.g_inst_count, gc + 1) + go = nil + GC.start + assert_equal(RBA::GObject.g_inst_count, gc) + + end + end