Re: GObject Destruction



On Tue, Dec 18, 2012 at 07:19:33PM +0100, Nicola Fontana wrote:
> I always free dynamic stuff (included allocated memory) in
> dispose(), not in finalize(), protecting it from double calls.
> There you should have a still valid instance to query.

GObject gives the following, somewhat contradictory, requirements for
dispose():

    When dispose ends, the object should not hold any reference to any
    other member object. The object is also expected to be able to
    answer client method invocations (with possibly an error code but no
    memory violation) until finalize is executed. dispose can be
    executed more than once.  dispose should chain up to its parent
    implementation just before returning to the caller.

If you free everything in dipose() you may have troubles with the ‘able
to answer client requests’ part.

All members that can be set and get by object users must be freed in
dispose() in order to break reference cycles, that is clear.  If these
members can be NULL also during normal object operation it is perfectly
safe and the object can be expected to still operate normally (otherwise
you may be on a somewhat thin ice).

Internal data members that cannot be set from outside are, IMO, better
freed in finalize() because then you do not need to worry about the
‘able to answer client requests’ part in case they were already freed.

The best protection against double-freeing things is the simple rule: a
pointer may be only valid or NULL.  Never free anything (or release your
reference that may be last) and leave the pointer set.

Regards,

Yeti



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