[plt-scheme] references to deleted objects

From: Ron Stanonik (stanonik at Cogsci.ucsd.edu)
Date: Thu Nov 7 14:50:50 EST 2002

We're using drscheme's class system to interface to a graphical system.

For example 

  (define o (make-object oval% dynapad))

creates an oval on the graphic surface.  The class oval% has a field
which holds a reference (scheme_make_cptr) to a C++ object which implements
the oval on the graphic surface.

The graphical objects are manipulated from drscheme by messages.
For example,

  (send o slide 10 20)
  (send o fill "red")

  (send o delete)

The last example deletes the oval from the graphic surface.
Of course the scheme object remains as long as there is a
reference to it and that leads to problems because messages
will continue to be sent to those references.  (For example,
the scheme object might be the destination of a hyperlink,
or it might be on a list of objects to be notified about
some event.)

Two solutions come to mind, but maybe there is something better.

1) When creating a reference to an instance of oval%, also create
   a reference from the instance back to the referrer (probably kept
   in a field in oval%), and have the delete method update referrers
   to remove their reference to the instance.

2) Add a "deleted" field to oval%, initialize it to #t, have the
   delete method set deleted to #f, and have the other oval% methods
   check deleted before trying to operate on the missing graphic object.

2.5) Add a "deleted" field and replace send with a send that checks
   deleted before actually sending the message.

Surely others have had to deal with references to deleted scheme objects.

Any suggestions?


stanonik at cogsci.ucsd.edu

Posted on the users mailing list.