[Newbies] lost socket

Yoshiki Ohshima yoshiki at vpri.org
Wed Sep 24 08:01:39 UTC 2008


At Wed, 24 Sep 2008 17:42:08 +1200,
Aidan Gauland wrote:
> 
> Hello,
> 
>   I've been playing around with the Socket class, and the code I was running 
> in an EToys script crashed a few times (I did try it in a workspace first, it 
> was the code I wrote to talk to other players that crashed), and the temporary 
> sockets it created never got destroyed.  Is this going to cause problems for 
> me later?

  The short answer is: "probably not".

  There is some missing information here:

  - What do you mean by "crashed"?  Did that raise the pink notifiers?
  - What do you mean by "never got destroyed"?  Did you set a break point in the
    #destroy method but didn't reach, or looked at the socketHandle
    instance variables of Socket objects?

And,

  - Do you quit or save and quit the image after it happened a few
    times (or in between of them)?

When a Socket object is created, an accompanying data structure
(actual socket created by socket() system call or its equivalent and
some extra information for interfacing with Squeak) in the VM is
created, and the socketHandle instance variable of the Socket object
is filled with the "handle" to the data structure.  When the #destroy
method is called on the Socket, the internal data structure is also
destroyed and cleaned up.

Also, when the Socket object is no longer referenced from anywhere in
the image and garbage collected, the finalization process is invoked
and does the same cleaning up.  This means that if your program has a
bug and #destroy method somehow didn't get sent to your Socket object,
you can cut the references to the Socket object and make the system
take care of the clean up.  (Or, manually send #close from the
inspector of the Socket object; Try to print-it the following: Socket
allInstances.)

When Squeak quits, the data structure in the VM will be gone as there
is no way to save them, but the Socket object in the image may be
still referenced by some other objects and saved in the .image file.
Next time you launch the image, the Socket object is still there, with
the socketHandle instance variable kept.  However, because there is no
accompanying data structure in the VM at this point, the Socket object
is "invalid" and won't do anything useful (or harmful).  Suppose a
Socket is created and listen on a port, and you save and quit the
image.  Next time you launch the image, the Socket object is there,
but the port is available so you can create a new Socket and start
listening on the port in the new session.

What you can do is to write code so that unused Socket object gets
#close message properly, and make sure the Socket object is
unreferenced when it happens.  And if your code involves the saving
and launching image, make sure that necessary Socket objects are
initialized in the new Squeak session.

-- Yoshiki


More information about the Beginners mailing list