Home > Archive > Smalltalk > March 2005 > "destructors" in St
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
"destructors" in St
|
|
| Fernando 2005-03-17, 4:00 pm |
| Hi,
I know that there aren't destructors in St, but what is the
equivalent?
Specificly, where and when am I suposed to free scarce resources (such
file handles or db connections) that I might have allocated during
creation or usage of a class?
Thanx! :-)
| |
|
| the appdevguide.pdf that comes with the VW distribution there is a
chapter on "Weak References and Finalization" which you should find
helpful.
-Charles
| |
| Udo Schneider 2005-03-17, 8:58 pm |
| Fernando,
if you want to explicitly perform some actions before a object gets
GC'es (normally freeing *external* resources) you should take a look at
finalization.
WeakCollections are another part of the same game ... e.g. if you want
to hold a collection of objects where keeping them in the collection
doesn't prevent the GC from GC'ing it.
More info here:
http://www.object-arts.com/Educatio...inalization.htm
http://www.object-arts.com/Educatio...Collections.htm
http://www.object-arts.com/Educatio...ns/Weakling.htm
CU,
Udo
Fernando wrote:
> Hi,
>
> I know that there aren't destructors in St, but what is the
> equivalent?
>
> Specificly, where and when am I suposed to free scarce resources (such
> file handles or db connections) that I might have allocated during
> creation or usage of a class?
>
> Thanx! :-)
| |
|
|
|
|
| giorgioferraris@elevensoft.it 2005-03-18, 8:57 am |
|
Fernando wrote:
> Hi,
>
> I know that there aren't destructors in St, but what is the
> equivalent?
>
> Specificly, where and when am I suposed to free scarce resources
(such
> file handles or db connections) that I might have allocated during
> creation or usage of a class?
>
> Thanx! :-)
Hi, Fernando,
In the case you are talking about (file handles and DB connection), I
think it's not generally the better thing to do to wait for the garbage
collector service's for cleaning up (as done using the weak pinter)
because this can happen after a longer than suspected time.
You should usually release those resources as soon as your application
logic does not need them anymore.
Probably best would be to ensure (using an ensure: method) that the
resources are discarded, so also on an error condition (as in the case
of an error on reading or writing file) you will have the system doing
the clean up, becouse leaving open connections or open files could give
you some trouble.
Releasing resources on your case means closing the connection or
closing the file.
here is an example:
stream := myFileName asFilename readstream.
[
.... do my reading here...
]ensure [stream isNil ifFalse: [stream close]].
hth
Ciao
Giorgio Ferraris
| |
| Udo Schneider 2005-03-18, 8:57 am |
| giorgioferraris@elevensoft.it wrote:
> In the case you are talking about (file handles and DB connection), I
> think it's not generally the better thing to do to wait for the garbage
> collector service's for cleaning up (as done using the weak pinter)
> because this can happen after a longer than suspected time.
You can as well mix both approaches. You can provide a #free method
which frees external resources.
This method can either be called directly (freeing the resources
explicitly and disabling the implictily freeing through finalization) or
by #finalize.
E.g. some Dolphin classes (e.g. Canvas) are using this approach.
Normally you don't have to care about Canvases. Once the last reference
is gont the GC will free the canvas (at some time) and send #finalize.
> You should usually release those resources as soon as your application
> logic does not need them anymore.
On the other hand if you are creating a huge amount of Canvases you
might explicitly freeing them if you don't need them any more. This can
give you some advantages in regards to GDI resources.
> Probably best would be to ensure (using an ensure: method) that the
> resources are discarded, so also on an error condition (as in the case
> of an error on reading or writing file) you will have the system doing
> the clean up, becouse leaving open connections or open files could give
> you some trouble.
> Releasing resources on your case means closing the connection or
> closing the file.
> here is an example:
>
> stream := myFileName asFilename readstream.
> [
> .... do my reading here...
> ]ensure [stream isNil ifFalse: [stream close]].
>
You might as well add another layer on top of it.
Define a method like #readWhile: :
Filename>>#readWhile: anOperation
| stream |
stream := self readStream.
(anOperation value: stream) ensure: [stream notNil ifTrue: [stream close]]
This would allow you to write something like:
myFilename asFilename readWhile: [ :stream | .....].
Just my 5 € cents.
CU,
Udo
|
|
|
|
|