This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Pseudo-weak?


forcer <forcer@mindless.com> writes:

> Is it possible to designate function to be called when an object
> isn't referenced anymore except for one (some?) "pseudo-weak"
> locations? E.g. the same behavior as with weak references, except
> that the object does not get garbage collected.


I'm not sure if I understand what you mean but:

a) You can use guardians: You register your object to a guardian so that
you can ask the guardian later if your object is still alive.  If its
dead the guardian still holds a reference to it so the stored data is
still valid.  If you remove it from the guarded list, it will be
gc'ed.

b) Call your own finalization function (only possible on C level):
Sometimes using a guardian is not possible because you don't
have control over the object and you can't associate additional
data to it.   Then you can encapsulate the foreign object into 
a structure, for example a linked list.  After each gc you iterate
over all objects in your list.  If one of them has been gc'ed you
remove the node from the list and call the appropriate finalization function
(close a file pointer etc.). 


For example:

struct myGuardian {
	SCM some_foreign_object;

	struct container container;
	struct myGuardian *next;
}

/* function called directly after gc */
finalize()
{
	forAll myGuardians
		if myGuardian has been gc'ed
			myGuardian.container.finalize();
		removeCurrentGuardFromGuardianList;
}


The myGuardian.container.finalize() function can be thought as a
function that will be called directly by the garbage collector when the
associated `some_foreign_object' has been gc'ed.

In other words:  The container `container' asks the garbage collector
to call its finalize() function whenever its foreign_object has been
gc'ed.  This is a little bit more than guardians provide.  You can't ask
a guardian to "call the function i give you with the object as its argument
when the object is zombified".  You can only pass the object, not the call-back
function.


Jost





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]