Re: g_object_new(G_TYPE_OBJECT,NULL) failed

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

 



Yueyu Lin wrote:
Hi,
    After reading the G-lib system carefully,I think I understand the GLib system. So I writer the following codes:
[...]

Here are a few comments:

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

You might want to put `pet->priv = g_new0 (PrivStruct, 1);` in `pet_instance_init ();' this way, if `IS_PET ()' is true, you always know that the data is already there.

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

This code will crash; you need to just use `parent_class->finalize (object)' instead of using a wild pointer ;-p

(Note that parent_class was obtained with g_object_class_peek_parent()
in class_init; so if there is an instance; you know that `parent_class'
is assigned and you dont need to check it).

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

Hmmm, I didn't read up on this new "constructor" idea; but I'm sure its not nescisary for your code, also; the common practice is to simply use a global `static GObjectClass *parent_class' and assign it once class-wide in your pet_class_init function (I guess this is just a slight optimization though).

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

Hmmm, I guess this is compiling because you are using a c++ compiler ? Anyway; I guess it should work anyway.

One thing you'll have to do is make a call to `g_type_init()' before
using the type system.

Cheers,
                             -Tristan
_______________________________________________

gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux