Skip to content

Instantly share code, notes, and snippets.

@kugel-
Last active November 6, 2016 21:58
Show Gist options
  • Save kugel-/0b051d9a22516ea53b48d33009d11175 to your computer and use it in GitHub Desktop.
Save kugel-/0b051d9a22516ea53b48d33009d11175 to your computer and use it in GitHub Desktop.
/*
* cc -g $(pkg-config --cflags ruby-2.3 glib-2.0 gtk+-3.0) test.c $(pkg-config --libs ruby-2.3 glib-2.0 gtk+-3.0)
*/
#include <ruby/ruby.h>
#include <gtk/gtk.h>
#include <glib-object.h>
const char script[] = " \n"
"require 'rubygems' \n"
"require 'gir_ffi' \n"
" \n"
"class ExtensionRbFFI < GObject::Object \n"
" \n"
" GirFFI.define_type(ExtensionRbFFI) do |info| \n"
" info.g_name = 'ExtensionRbFFI' \n"
" bits = GObject::ParamFlags[:readable] | \n"
" GObject::ParamFlags[:writable] \n"
" info.install_property GObject.param_spec_object('object', \n"
" 'object', \n"
" 'object', \n"
" GObject::Object.gtype, \n"
" bits) \n"
" end \n"
" \n"
" def initialize \n"
" super \n"
" end \n"
"end \n"
" \n"
"ExtensionRbFFI.gtype.to_i";
int main(int argc, char *argv[])
{
VALUE ret;
GType t;
GObject *prop, *obj;
int ex_state;
gtk_init(&argc, &argv);
ruby_init();
ruby_init_loadpath();
ret = rb_eval_string_protect(script, &ex_state);
g_assert(ex_state == 0);
t = NUM2LONG(ret);
prop = G_OBJECT(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
g_object_ref_sink(prop);
g_assert(prop->ref_count == 1);
obj = g_object_new(t, "object", prop, NULL);
/* I would expect 2 */
g_assert(prop->ref_count == 3);
g_object_unref(obj);
/* Calling rb_gc() or not doesn't make a difference */
rb_gc();
/* ERROR */
g_assert(prop->ref_count == 1);
g_object_unref(prop);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment