Is there a hook (a method) which is called, if the vm ends?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a hook (a method) which is called, if the vm ends?

Joachim Jaeckel
Hello,

'finalize' is called, if the object will be garbage collected. But is
there also a method, or exist a hook, that a method could be called, if
the vm ends?

I want to close my open files in a method of my object and write a last
message into the files, before the vm ends.

Thanks in advance,
Joachim.


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Is there a hook (a method) which is called, if the vm ends?

Paolo Bonzini-3
On 07/02/2009 02:39 PM, Joachim Jaeckel wrote:
> Hello,
>
> 'finalize' is called, if the object will be garbage collected. But is
> there also a method, or exist a hook, that a method could be called, if
> the vm ends?
>
> I want to close my open files in a method of my object and write a last
> message into the files, before the vm ends.

Files are closed automatically.

However, you can use

ObjectMemory addDependent: self.

and then "self" will get a bunch of events dispatched via the #update:
method.  grep "update:" in kernel/*.st for an example of its usage.

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Is there a hook (a method) which is called, if the vm ends?

Joachim Jaeckel
Thanks Paolo,

> Files are closed automatically.

Ok, but additionally, I was looking to write a summary-line at the end
of the files before they are closed.

> However, you can use
>
> ObjectMemory addDependent: self.
>
> and then "self" will get a bunch of events dispatched via the #update:
> method.  grep "update:" in kernel/*.st for an example of its usage.

I tried it and found out (for me) that "ObjectMemory addDependent: self"
should be send in a class-method of "self". (in the case, that I did
that from an Object-method, nothing happens.) -> and so I found it in
the kernel/*.st files also.

Even #update: has to be a class method.

But how could I call from the class, a method from the instance?

You did that in FileWrapper like this:

aspect == #aboutToQuit ifTrue: [self broadcast: #release]

and the "Object" instance is the only place, where #broadcast: is
defined. (I was first looking for an implementation of #broadcast as a
class-method, but I only found the one in the Object-instance.)

But if I did it, I always get a MessageNotUnderstood Exception...
(Would it be an idea, to maintain an OrderedCollection in the class with
all the instances of this class, to iterate over all these in the case,
the #update: class-method is getting the #aboutToQuit?)

Regards,
Joachim.


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Is there a hook (a method) which is called, if the vm ends?

Stefan Schmiedl
On Fri, 03 Jul 2009 00:05:22 +0200
Joachim Jaeckel <[hidden email]> wrote:

> (Would it be an idea, to maintain an OrderedCollection in the class
> with all the instances of this class, to iterate over all these in
> the case, the #update: class-method is getting the #aboutToQuit?)

I'm not sure if that is the solution to your original problem,
but this idea is so useful that you're not the first to have it:

GNU Smalltalk ready

st> Object allInstances size
0
st> Object new class allInstances size
1

s.


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Is there a hook (a method) which is called, if the vm ends?

Paolo Bonzini-3
In reply to this post by Joachim Jaeckel

> I tried it and found out (for me) that "ObjectMemory addDependent: self"
> should be send in a class-method of "self". (in the case, that I did
> that from an Object-method, nothing happens.) -> and so I found it in
> the kernel/*.st files also.
>
> Even #update: has to be a class method.

No, it does not need to be.  Of course whatever is passed to
#addDependent: (self can be either a class or an instance) should match
the place where you define #update: (class or instance)

> But if I did it, I always get a MessageNotUnderstood Exception...
> (Would it be an idea, to maintain an OrderedCollection in the class with
> all the instances of this class, to iterate over all these in the case,
> the #update: class-method is getting the #aboutToQuit?)

If the above does not work, you can use a WeakSet instead.  If you use
an OrderedCollection nothing will be garbage collected---but then maybe
that is okay for you?

Strange that you get a doesNotUnderstand though.

An aside: all instance methods of Objects are also available as class
methods.  That's because Object is a superclass of Behavior, which is a
superclass of Class, which is a superclass of Object class, which is
where class methods are defined in.  Dually, there is actually no method
in Object class, or maybe almost none; class methods for Object are
defined in Behavior and its subclasses.

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Is there a hook (a method) which is called, if the vm ends?

Paolo Bonzini-3
In reply to this post by Stefan Schmiedl

> GNU Smalltalk ready
>
> st>  Object allInstances size
> 0
> st>  Object new class allInstances size
> 1

But #allInstances is slow.  It takes about 10-20 ms to do it no matter
how many instances are there.

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Is there a hook (a method) which is called, if the vm ends?

Joachim Jaeckel
In reply to this post by Paolo Bonzini-3
Thanks for your help!

It works (with the Object-methods method - Maybe it was late yesterday
and I was too tired...)

Thanks!
Joachim.


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk