创建gobject时的GLib-GObject-Warning

时间:2013-07-04 13:17:49

标签: c thrift glib flume

我正在尝试在c(c_glib)中创建一个flume-thrift客户端,但是无法创建应该发送到服务器的gobject。我在main.c的行中得到以下错误:

`GLib-GObject-WARNING **: IA__g_object_new_valist: object class `ThriftFlumeEventType' has no property named `timestamp'`

flume_types.h和flume_types.c中的代码是从thrift自动生成的。如果您需要查看更多代码,请告诉我。我很感谢能得到的所有帮助!

flume_types.h中的部分代码:

struct _ThriftFlumeEvent
{ 
  ThriftStruct parent;

  /* public */
  gint64 timestamp;
  gboolean __isset_timestamp;
  Priority priority;
  gboolean __isset_priority;
  GByteArray * body;
  gboolean __isset_body;
  gint64 nanos;
  gboolean __isset_nanos;
  gchar * host;
  gboolean __isset_host;
  GHashTable * fields;
  gboolean __isset_fields;
};
typedef struct _ThriftFlumeEvent ThriftFlumeEvent;


GType thrift_flume_event_get_type (void);
#define TYPE_THRIFT_FLUME_EVENT (thrift_flume_event_get_type())

flume_types.c中的部分代码:

void 
thrift_flume_event_instance_init (ThriftFlumeEvent * object)
{
  printf("thrift_flume_event_instance_init");

  /* satisfy -Wall */
  THRIFT_UNUSED_VAR (object);
  object->timestamp = 0;
  object->__isset_timestamp = FALSE;
  object->__isset_priority = FALSE;
  object->body = NULL;
  object->__isset_body = FALSE;
  object->nanos = 0;
  object->__isset_nanos = FALSE;
  object->host = NULL;
  object->__isset_host = FALSE;
  object->fields = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  object->__isset_fields = FALSE;
}

GType
thrift_flume_event_get_type (void)
{
  static GType type = 0;

  if (type == 0)
  {
    static const GTypeInfo type_info =
    {
      sizeof (ThriftFlumeEventClass),
      NULL, /* base_init */
      NULL, /* base_finalize */
      (GClassInitFunc) thrift_flume_event_class_init,
      NULL, /* class_finalize */
      NULL, /* class_data */
      sizeof (ThriftFlumeEvent),
      0, /* n_preallocs */
      (GInstanceInitFunc) thrift_flume_event_instance_init,
      NULL, /* value_table */
    };

    type = g_type_register_static (THRIFT_TYPE_STRUCT,
                                   "ThriftFlumeEventType",
                                   &type_info, 0);
    type.timestamp;
  }

  return type;
}

main.c中部分代码:

  gpointer eventObj = g_object_new(TYPE_THRIFT_FLUME_EVENT,
                                     "timestamp", 0,
                                     "__isset_timestamp", 0,
                                     "priority", 0,
                                     "__isset_priority", 0,
                                     "body", 0,
                                     "__isset_body", 0,
                                     "nanos", 0,
                                     "__isset_nanos", 0,
                                     "fields", 0,
                                     "__isset_fields", 0,
                                     0);

3 个答案:

答案 0 :(得分:0)

GObject属性是 NOT C结构成员。您需要在klass init函数(https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html)中通过g_object_install_property安装它们。您还需要从GObject(或从GObject派生的任何结构)派生对象。

注意:用C语言导出,它必须将父项作为其第一个成员,以便可以无缝地进行转换(不是指向GObject的指针!)

你应该读一本关于这个主题的书,它会变得非常复杂。

答案 1 :(得分:0)

这是解决问题的方法:

ThriftFlumeEvent *event = g_object_new(TYPE_THRIFT_FLUME_EVENT, 0);
event->timestamp = (gint64)t_stamp.tv_sec * 1000;
event->__isset_timestamp = TRUE;
event->priority = priority;
event->__isset_priority = TRUE;
...

答案 2 :(得分:0)

这在Thrift 0.9.2中发生了变化:C(GLib)编译器生成的用于表示Thrift结构的对象现在将其成员公开为GObject属性,从而避免了修改对象的风险(和差)实践实例结构直接。

从0.9.2开始,初始化struct对象的首选方法基本上就是海报最初的预期:

ThriftFlumeEvent *event =
  g_object_new (TYPE_THRIFT_FLUME_EVENT,
                "timestamp", (gint64)t_stamp.tv_sec * 1000,
                "priority",  priority,
                ...
                NULL);

请注意,__isset_字段由对象本身管理,不应直接设置。