πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.csharpbuilder.language.csharp
4 messages
4 total messages Started by Francois PIETTE [ICS - MidWare] Tue, 25 Jul 2006 18:48
Where to cleanup an object instance
#231
Author: Francois PIETTE [ICS - MidWare]
Date: Tue, 25 Jul 2006 18:48
19 lines
1718 bytes
I'm porting Delphi code to C#.
In a Delphi class, you cleanup a class instance from the destructor. But in
C#, there is no destructor.
So where do I put cleanup code (such as closing database connection) ?
Should I put the cleanup code in Finalize() ?
Can I explicitely call Finalize() when I'm done with an object ?
I don't want the cleanup code to be called when the object instance is 
garbage collected. I want it to be executed when I'm done with the object 
instance.

Maybe a better design is to create my Cleanup() method and call it from
where I used to call Delphi's destructor ?

Any advice appreciated.
-- 
francois.piette@overbyte.be
The author for the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be
Re: Where to cleanup an object instance
#232
Author: Marc Rohloff TeamB
Date: Tue, 25 Jul 2006 22:11
15 lines
1723 bytes
On Tue, 25 Jul 2006 20:48:44 +0200, Francois PIETTE [ICS - MidWare]
wrote:

> I'm porting Delphi code to C#.
> In a Delphi class, you cleanup a class instance from the destructor. But in
> C#, there is no destructor.

Generally you want to implement a finalizer and the IDisposable
interface. Its a little tricky to do it correctly but if you do a web
search on the IDisposable pattern you should find plenty of good
resources.

-- 
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
Re: Where to cleanup an object instance
#233
Author: Francois PIETTE [ICS - MidWare]
Date: Wed, 26 Jul 2006 16:15
19 lines
1702 bytes
>> I'm porting Delphi code to C#.
>> In a Delphi class, you cleanup a class instance from the destructor. But 
>> in
>> C#, there is no destructor.
>
> Generally you want to implement a finalizer and the IDisposable
> interface. Its a little tricky to do it correctly but if you do a web
> search on the IDisposable pattern you should find plenty of good
> resources.

I found this one:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconfinalizedispose.asp

Contribute to the SSL Effort. Visit http://www.overbyte.be/eng/ssl.html
--
francois.piette@overbyte.be
The author for the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be
Re: Where to cleanup an object instance
#234
Author: Joanna Carter [TeamB]
Date: Thu, 27 Jul 2006 08:38
31 lines
2217 bytes
"Francois PIETTE [ICS - MidWare]" <francois.piette@overbyte.be> a οΏ½crit dans 
le message de news: 44c66807@newsgroups.borland.com...

| I'm porting Delphi code to C#.
| In a Delphi class, you cleanup a class instance from the destructor. But 
in
| C#, there is no destructor.
| So where do I put cleanup code (such as closing database connection) ?
| Should I put the cleanup code in Finalize() ?
| Can I explicitely call Finalize() when I'm done with an object ?
| I don't want the cleanup code to be called when the object instance is
| garbage collected. I want it to be executed when I'm done with the object
| instance.
|
| Maybe a better design is to create my Cleanup() method and call it from
| where I used to call Delphi's destructor ?

Unless you are using classes that implement IDisposable, those which hold 
Windows resources like file or UI handles, etc, you don't need to do any 
cleanup. In fact doing cleanup really fouls up the garbage collection and 
can slow your program due to memory fragmentation.

If your code doesn't use Windows resources and you think deterministic 
cleanup is important, then you need to thnk about redesigning to work with, 
not against the GC.

Joanna

-- 
Joanna Carter [TeamB]
Consultant Software Engineer 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads