PC Review


Reply
Thread Tools Rate Thread

C# Exception cleanup resource management like C++?

 
 
Mike N
Guest
Posts: n/a
 
      16th Feb 2005
Coming from a C++ background, are destructors called for all local
objects after a C# exception?

C++:

myproc() {

CSingleLock protect(&m_myCritsec, FALSE);
if (!protect.Lock())
{
// Handle error
}

// more stuff that can throw an exception

}

.... You know that the critical section for 'protect' is released
properly because its destructor will be called regardless of an
exception.

If I understand C#, you could not develop a similarly functioning
CSingleLock class because the destructor is called only at some later
random point by the garbage collection. So to properly release
semaphores, etc, you must use a try...finally construct everywhere,
manually track everything that might have been locked/used and make
sure it is released?

 
Reply With Quote
 
 
 
 
Sean Hederman
Guest
Posts: n/a
 
      16th Feb 2005
You could use the lock statement, or, if the object implements IDisposable
you can use the using construct

C#:
myproc() {
lock(this) {
// Do stuff inside lock, exceptions will release the lock.
}
}
or:
myproc() {
using(new MySingleLock(args)) {
// Do stuff, MySingleLock.Dispose will be automatically called on
exception and exiting scope
}
}

"Mike N" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Coming from a C++ background, are destructors called for all local
> objects after a C# exception?
>
> C++:
>
> myproc() {
>
> CSingleLock protect(&m_myCritsec, FALSE);
> if (!protect.Lock())
> {
> // Handle error
> }
>
> // more stuff that can throw an exception
>
> }
>
> .... You know that the critical section for 'protect' is released
> properly because its destructor will be called regardless of an
> exception.
>
> If I understand C#, you could not develop a similarly functioning
> CSingleLock class because the destructor is called only at some later
> random point by the garbage collection. So to properly release
> semaphores, etc, you must use a try...finally construct everywhere,
> manually track everything that might have been locked/used and make
> sure it is released?
>



 
Reply With Quote
 
Mike N
Guest
Posts: n/a
 
      16th Feb 2005
On Wed, 16 Feb 2005 21:35:04 +0200, "Sean Hederman"
<(E-Mail Removed)> wrote:

>C#:
>myproc() {
> lock(this) {
> // Do stuff inside lock, exceptions will release the lock.
> }
>}


Thanks! This one appears very useful!


>or:
>myproc() {
> using(new MySingleLock(args)) {
> // Do stuff, MySingleLock.Dispose will be automatically called on
>exception and exiting scope
> }
>}


Thanks, this was a general solution I was looking for; I had skimmed
over the 'using' keyword, thinking it was namespace scope only.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in Async Delegate Causing Resource Cleanup in Others Travis Parks Microsoft C# .NET 2 17th Oct 2010 06:50 AM
Out-of-process State Management Session Cleanup knyghtfyre@gmail.com Microsoft ASP .NET 5 19th Sep 2007 07:01 PM
Re: Getting exception while using Exception Management block David Levine Microsoft C# .NET 0 9th Sep 2004 09:25 PM
Session_end, User Identity and resource cleanup Dan Walls Microsoft ASP .NET 3 16th Apr 2004 05:42 PM
WinForms Cleanup - Object Disposed Exception Jeff M Microsoft Dot NET Compact Framework 0 8th Aug 2003 11:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:36 AM.