"destructors" in St

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

"destructors" in St

Fernando Rodríguez
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! :-)


Reply | Threaded
Open this post in threaded view
|

Re: "destructors" in St

Schwab,Wilhelm K
Fernando,

> 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?

Search the archives, D4 docs, and the wiki for finalization.  Also note
that you might also need to pay attention to the system shutdown events,
as finalization might not happen before shutdown (and might "never"
happen).  The archives will help there too, as long as you know to look
for it.

Have a good one,

Bill


--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: "destructors" in St

OCIT
In reply to this post by Fernando Rodríguez
the appdevguide.pdf that comes with the VW distribution there is a
chapter on "Weak References and Finalization" which you should find
helpful.

-Charles


Reply | Threaded
Open this post in threaded view
|

Re: "destructors" in St

Udo Schneider
In reply to this post by Fernando Rodríguez
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/EducationCentre/Patterns/Finalization.htm
http://www.object-arts.com/EducationCentre/Patterns/WeakCollections.htm
http://www.object-arts.com/EducationCentre/Patterns/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! :-)


Reply | Threaded
Open this post in threaded view
|

Re: "destructors" in St

Udo Schneider
I forgott this one:

http://www.object-arts.com/EducationCentre/Overviews/WeakReferencesAndFinalization.htm


CU,

Udo


Udo Schneider wrote:

> 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/EducationCentre/Patterns/Finalization.htm
> http://www.object-arts.com/EducationCentre/Patterns/WeakCollections.htm
> http://www.object-arts.com/EducationCentre/Patterns/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! :-)


Reply | Threaded
Open this post in threaded view
|

Re: "destructors" in St

Udo Schneider
In reply to this post by Udo Schneider
I forgott this one:

http://www.object-arts.com/EducationCentre/Overviews/WeakReferencesAndFinalization.htm


CU,

Udo


Udo Schneider wrote:

> 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/EducationCentre/Patterns/Finalization.htm
> http://www.object-arts.com/EducationCentre/Patterns/WeakCollections.htm
> http://www.object-arts.com/EducationCentre/Patterns/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! :-)


Reply | Threaded
Open this post in threaded view
|

Re: "destructors" in St

Giorgio Ferraris
In reply to this post by Fernando Rodríguez
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


Reply | Threaded
Open this post in threaded view
|

Re: "destructors" in St

Udo Schneider
[hidden email] 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