【问题标题】:Ruby Data_Get_Struct error wrong argument expect DataRuby Data_Get_Struct 错误参数错误期望数据
【发布时间】:2015-01-22 10:00:33
【问题描述】:

我正在用 C 语言编写一个带有一些非常简单的类的小 ruby​​ 模块:

typedef struct window_t {
   GtkWidget * widget;
}
static void c_window_struct_free(window_t *c)
{
  if(c)
  {
    ruby_xfree(c);
  }
}
static VALUE c_window_struct_alloc( VALUE klass)
{
  return Data_Wrap_Struct(klass, NULL, c_window_struct_free,ruby_xmalloc(sizeof(window_t)));
}

VALUE c_window = rb_define_class_under(m_rtortosa, "Window", c_widget)
rb_define_method(c_window, "set_title",RUBY_METHOD_FUNC(window_set_title), 1);
//For each class I don't rewritte any "new" or "initialize" function. I let the default 

当我的模块被初始化时,一个 gtk 窗口被创建,我有一个用于这个模块的 ruby​​ 方法调用:

static VALUE rtortosa_window(VALUE self)
{
  VALUE win;
  VALUE m_rtortosa = rb_const_get( rb_cObject, rb_intern( "Rtortosa" ) );
  VALUE cWindow = rb_const_get_at( m_rtortosa, rb_intern("Window") );
  win = rb_class_new_instance(0, NULL, backbone.rb_objects.cWindow);
  window_t *w;
  Data_Get_Struct(win,window_t, w);
  w->widget = backbone.window.widget;
  return win;
}

当我从 ruby​​ 调用 rtortosta_window 时出现问题,它会抛出如下错误:

wrong argument type Rtortosa::Window (expected Data) (TypeError)

经过调查,此消息来自 Data_Get_Struct 函数。

我看不出我做错了什么,我有一个以相同方式编写的笔记本课程,但它按预期工作。

【问题讨论】:

    标签: ruby ruby-c-extension


    【解决方案1】:

    我忘记将alloc函数绑定到类:

    rb_define_alloc_func(c_window, c_window_struct_alloc);
    

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      相关资源
      最近更新 更多