Re: gtk_object_unref



>
> "Jean-Christophe Berthon" <jean-christophe berthon cgey com> writes:
> > (I've read some older threads about it - like the one about the
container -
> > but in my case my objects and points are drawings on a gtk-canvas and I
want
> > to ref and unref some of the GtkCanvasItem that are shared...)
> >
> >
> > I hope I've been clear enough, if you need more explanation, tell me
what
> > you didn't understand and I will try to say it differently.
>
> Say you create the canvas item to give it to object A - do you also
> reference it?

    first thank you all for your answers.
Yes you right I didn't reference it.


>
> If not, then you are assuming ownership of the "floating" reference,
> which isn't allowed. The canvas will then strip the floating reference
> off the item when you add the item to the canvas; and object A will
> end up not owning any reference at all.
>
> If you do this:
>
>  obj = whatever_gtk_object_new ();
>
> Then the immediate situation is that "obj" has one floating reference,
> and zero owned references. To remove the floating reference, do this:
>
>  gtk_object_sink (obj);
>
> That will finalize the object if it hasn't already been finalized.
>
> What the canvas does is this:
>
>  gtk_object_ref  (obj);
>  gtk_object_sink (obj);
>
> So then "obj" has one reference owned by the canvas, and zero floating
> references.
>
> Anyhow, short answer, you need another gtk_object_ref(), rather than
> taking ownership of the floating reference. No one can ever own the
> floating reference; if you want to call unref on a GtkObject, you must
> first call ref; there are no exceptions, even if you created the
> object yourself.
>

OK. I didn't really understand everything (esp. about the floating
reference, I don't exactly what this thing mean or is...). But I've tryed to
add to my code a gtk_object_sink just after the creation of the canvas item.
Then I've tryed to add instead gtk_object_ref and gtk_object_sink. And
finally I've tryed gtk_object_ref only instead of the previous two commands.
But in all this case I got the warnings.

But I want to precise one thing.
The unref operation occur in a callback of the canvas item. So one of them
receive an action from the user and I have to unref this item and a second
one (passed with the void* of the callback).
The item that fired the event has a ref_count of 3 (instead I would expect
1) while the other one has a ref_count of 1 (which should be normal).
I'm applying the unref to both of them, so after the unref execute there
ref_count decremente of 1 (so respectively to 2 and 0). I'm getting a
warning only on the second item but both are destroyed!!! (I guess this job
is done when leaving the callback as at this moment I receive a second
warning (again about this ref_count).)

If you need I can send a extract of my code with all the involved parts.

Again thanks all for your help,
Have a nice day (or night)...

Best regards,
Jean-Christophe









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