[comp.lang.eiffel] What happens when you're gone and forgotten?

langley@yoyodyne.rtp.dg.com (03/19/90)

How do I write a class so that a routine will get called
every time the object is forgotten?

In particular, whenever the object becomes unreachable, or
is subjected to an explict forget operation, I want a class
routine to get called.

Stated another way, if I have a function that is called whenever
something is created, it seems like I should have a function
called when it is deleted too.  Alternatively, if creation is
implicit then destruction should be implicit too.  Being allowed
an explicit constructor, but no destructor, seems inconsistent.

People who have used C++ will know what I am talking about when
I ask "how do you define a destructor?"  Is it the case that
for some reason this sort of functionality isn't useful in Eiffel?
It seems to me that even in a GC-ed environment I might like to
have some way of keeping references to my own objects...

Mark Langley
langley@dg-rtp.dg.com

jos@cs.vu.nl (Jos Warmer) (03/19/90)

langley@yoyodyne.rtp.dg.com writes:


>How do I write a class so that a routine will get called
>every time the object is forgotten?

The class MEMORY from the support library has a function `dispose'.
Whenever an object is destroyed by the garbage collector this
function is called.

To get the behaviour you desire, inherit from class MEMORY and
redefine the `dispose' function.

The function is not exported, so it does not show up in the
class interface and therefore it is not described in the library
manual.
The only place where it is documented is in the `RELEASE 2.2 OVERVIEW'.


		      Jos Warmer
		      jos@cs.vu.nl
--
                                 Jos Warmer
				 jos@cs.vu.nl
				 ...uunet!mcvax!cs.vu.nl!jos

greg@ait.trl.oz (Greg Aumann) (03/20/90)

From article <1586@xyzzy.UUCP>, by langley@yoyodyne.rtp.dg.com:
> 
> How do I write a class so that a routine will get called
> every time the object is forgotten?
> 
> I ask "how do you define a destructor?"  Is it the case that
> for some reason this sort of functionality isn't useful in Eiffel?

I would very much like to have this sort of facility available for an
application that I am writing.  The application is written in Eiffel
and C and uses X windows.  There are two reasons that I would like to
have a destructor function.  One is to free memory that was malloc'ed
in the C part of the application but is linked to an Eiffel object.
The second is to free X resources in the X server which are also
similarly linked to an Eiffel object.  What I would like to be able to
do is to write a destructor which simply frees the non-Eiffel resources
when the corresponging Eiffel object is destroyed.  I realise that
such a facility can be dangerous but I think it is better to have it
than to try and free stuff explicitly (which is why there is a GC in
the first place).
-------------------------------------------------------------------------
Artificial Intelligence Systems         ACSnet:    greg@trlamct.trl.oz
Telecom Research Laboratories           Internet:  greg@trlamct.trl.oz.au
Melbourne, AUSTRALIA                    Voice:     +61 3 541 6222

plogan@mentor.com (Patrick Logan) (03/21/90)

In article <6025@star.cs.vu.nl> jos@cs.vu.nl (Jos Warmer) writes:
   To get the behaviour you desire, inherit from class MEMORY and
   redefine the `dispose' function.
   --
				    Jos Warmer
				    jos@cs.vu.nl
				    ...uunet!mcvax!cs.vu.nl!jos

Here's what I was missing from my previous posting, i.e. multiple
inheritance so that the "dispose" procedure is only called on objects
that need to use it.

Does Eiffel use this in the window system classes to protect X's C
allocated structures?

Where (else) does Eiffel use this? Is it always to deallocate C data?
-- 
Patrick Logan  uunet!mntgfx!plogan | 
Mentor Graphics Corporation        | 
Beaverton, Oregon 97005-7191   	   |

jos@cs.vu.nl (Jos Warmer) (03/22/90)

plogan@mentor.com (Patrick Logan) writes:

>In article <6025@star.cs.vu.nl> jos@cs.vu.nl (Jos Warmer) writes:
>   To get the behaviour you desire, inherit from class MEMORY and
>   redefine the `dispose' function.

>Does Eiffel use this in the window system classes to protect X's C
>allocated structures?

I checked the window system classes and found no dispose function
anywhere. So the answer is no. In my opinion this is shortcoming
of the library classes.
I think each class that allocates C structures should use the
dispose function to deallocate this structures.
Otherwise C data garbage will never be deallocated.

>Where (else) does Eiffel use this? Is it always to deallocate C data?

It can be used to deallocate C data, but also for other purposes.
E.g: if an object is associated with a file, the dispose routine could close
that file  (to avoid too many opened files).
You might also use it to trace the behaviour of the garbage collector
for one or more classes.

				 Jos Warmer
				 jos@cs.vu.nl