POV-Ray : Newsgroups : povray.off-topic : Learning C# : Re: Learning C# Server Time
29 Jul 2024 04:31:21 EDT (-0400)
  Re: Learning C#  
From: Orchid Win7 v1
Date: 29 Sep 2012 17:17:14
Message: <5067655a$1@news.povray.org>
On 29/09/2012 08:35 PM, Warp wrote:
> Orchid Win7 v1<voi### [at] devnull>  wrote:
>> Then again, this is from the same book that explains how to write
>> destructors (i.e., "don't")
>
> If C# is anything like Java in this regard, then destructors are pretty
> much useless

That was pretty much it, yes.

In particular, any objects that "this" points to might already have been 
destroyed, so you can't touch those. You also have to make sure you do 
/not/ cause any dead objects to become alive again by adding pointers to 
them. Destructors are also run in a separate thread, so you have to be 
careful about thread-safety... In short, writing a correct destructor is 
a nightmare.

> In C++ destructors are useful for more than just freeing memory. For
> example they are commonly used in things like locks: If a function needs
> to use a lock, it can make a static lock instance which frees the lock
> in its destructor, and it's guaranteed to be freed immediately when the
> function is exited, regardless of how it's exited (ie. no matter where
> the 'return' sentence is, or even if it's exited by throwing an
> exception). It's handy because you don't need to any special code to
> make sure that the destructor is called when the function is exited.

Yeah, if destructors behave in a deterministic way, I'd imagine there's 
a few useful things you can do with them.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.