g_object_new(G_TYPE_OBJECT,NULL) failed



Hi,
    After reading the G-lib system carefully,I think I understand the GLib system. So I writer the following codes:
#pet.h
#ifndef PET_H
#define PET_H


/**
 *Type macros
 */
typedef struct _Pet Pet;
typedef struct _PetClass PetClass;

/**
 *Type macros for private structure 
 */
typedef struct _PrivStruct PrivStruct;

struct _Pet{
  GObject parent;
  
  /*instance members*/
  /*private*/
  PrivStruct *priv;
};

struct _PetClass {
  GObjectClass parent;
};

/*Get type method*/

GType pet_get_type(void);

gint pet_get_ages(Pet *pet);

void pet_set_ages(Pet *pet,int ages);



/*define helper macros*/

#define TYPE_PET             (pet_get_type())
#define PET(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),TYPE_PET,PetClass))
#define PET_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),TYPE_PET,PetClass))
#define IS_PET(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),TYPE_PET))
#define IS_PET_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),TYPE_PET))
#define PET_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj),TYPE_PET,PetClass))
#endif

#################################################################3
#pet.c
#include <glib-object.h>
#include "pet.h"



struct _PrivStruct{
  gint ages;
};

gint pet_get_ages(Pet *pet){
  if((pet)&&(pet->priv)){
    return pet->priv->ages;
  }
  return 0;
}

void pet_set_ages(Pet *pet,int ages){
  if(pet){
    g_print("set ages, the address is %x",pet);
    if(!(pet->priv)){
      pet->priv = g_newa(PrivStruct,1);
    }
    pet->priv->ages = ages;
  }
}



static void pet_finalize(GObject *object) {
  Pet *self = (Pet*)object;
  GObjectClass *parent;
  g_free(self->priv);
  parent->finalize(object);
}

static GObject *pet_constructor(GType type,
				guint n_construct_properties,
				GObjectConstructParam *construct_properties){
  GObject *obj;
  {
    PetClass *klass;
    GObjectClass *parent_class;
    klass = PET_CLASS(g_type_class_peek(TYPE_PET));
    parent_class = G_OBJECT_CLASS(g_type_class_peek_parent(klass));
    obj = parent_class->constructor(type,
				    n_construct_properties,
				    construct_properties);
  }
 
  return obj;
}


static void pet_class_init(gpointer g_class,
		     gpointer g_class_data){
  g_print("init class \n");
  GObjectClass *gobject = G_OBJECT_CLASS(g_class);
  PetClass *klass = PET_CLASS(g_class);  
  gobject->constructor = pet_constructor;
  gobject->finalize = pet_finalize;
}

static void pet_instance_init(GTypeInstance *instance,
			      gpointer      g_class){
  g_print("hello init\r\n");
}

GType pet_get_type(){
  static GType type = 0;
  if(type == 0){
    g_print("regist pet type\r\n");
    static const GTypeInfo info = {
      sizeof(PetClass),
      NULL,/*base_init*/
      NULL,/*base_finalize*/
      pet_class_init,/*class_init*/
      NULL,/*class_finalize*/
      NULL,
      sizeof(Pet),
      0,/*n_preallocs*/
      pet_instance_init         /*instance_init*/
    };
    type = g_type_register_static(G_TYPE_OBJECT,
				  "LinyyPetType",
				  &info,0);
    g_print("the type id is %d",type);
    
  }
  return type;
}



int main(int argc,char *argv[]) {
  g_print("hello\r\n");
  GObject *object = g_object_new(G_TYPE_OBJECT,NULL);
  g_free(object);
  Pet *pet = g_object_new(TYPE_PET,NULL);
  pet_set_ages(pet,3);
  int age = pet_get_ages(pet);
  g_print("it's age is %d",age);
  g_free(pet);
  return 0;
}

#################
I compiled the file using the following command:
$:gcc `pkg-config --cflags gtk+-2.0` `pkg-config --cflags gtk+-2.0` pet.c -o pet
It compiled successfully.
Then when I run ./pet
The result is:
hello

(process:5007): GLib-GObject-CRITICAL **: g_object_new: assertion `G_TYPE_IS_OBJECT (object_type)' failed
regist pet type

(process:5007): GLib-GObject-CRITICAL **: gtype.c:2254: initialization assertion failed, use IA__g_type_init() prior to this function
the type id is 0
(process:5007): GLib-GObject-CRITICAL **: g_object_new: assertion `G_TYPE_IS_OBJECT (object_type)' failed
##################
It actually means that the g_object_new(G_TYPE_OBJECT,NULL) failed.I think it means that the object initialization failed.
I want to know why it happens?
How can I solve it?


-- 
Best regards,
Yueyu Lin




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]