user1766169
user1766169

Reputation: 1987

GLib-GObject-Warning when creating gobject

I am trying to create a flume-thrift client in c (c_glib) but have trouble creating the gobject that shall be sent to the server. I get the following error at the line in main.c:

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

The code in flume_types.h and flume_types.c is autogenerated from thrift. Tell me if you need to see more code. I am thankful for all the help I can get!

Parts of the code in 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())

Parts of the code in 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;
}

Parts of the code in 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);

Upvotes: 0

Views: 2249

Answers (3)

drahnr
drahnr

Reputation: 6886

GObject properties are NOT C struct members. You need to install them via g_object_install_property in your class init function https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html . You also need to derive object from GObject (or from any struct that is derived from a GObject).

Note: derived in C means, that it has to have the parent as its first member so it can be cast seamlessly (NOT a pointer to a GObject!)

You should read a book on the topic, it can get quite complex.

Upvotes: 0

user473305
user473305

Reputation:

This is changing in Thrift 0.9.2: Objects generated by the C (GLib) compiler to represent Thrift structs will now expose their members as GObject properties, obviating the risky (and poor) practice of modifying an object's instance structure directly.

Starting with 0.9.2 the preferred way to initialize a struct object will be essentially what the poster originally expected:

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

Note the __isset_ fields are managed by the object itself and should not be set directly.

Upvotes: 1

user1766169
user1766169

Reputation: 1987

This was the solution to the problem:

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;
...

Upvotes: 0

Related Questions