|
![](/i/fill.gif) |
On 29/09/2012 08:35 PM, Warp wrote:
> Orchid Win7 v1<voi### [at] dev null> 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
|
![](/i/fill.gif) |