The GC, destructors, exceptions and memory corruption

Jonathan M Davis jmdavisProg at gmx.com
Fri May 13 08:45:25 PDT 2011


On 2011-05-13 08:06, Michel Fortin wrote:
> On 2011-05-13 09:44:33 -0400, Don <nospam at nospam.com> said:
> > If the GC calls it, it's a finalizer, not a destructor.
> > 
> > Structs have destructors, and it's perfectly OK to throw inside them.
> 
> Really? What if you allocated the struct on the heap, as a member of a
> class, or in an array? The struct will be on the heap and its
> destructor will become the finalizer.
> 
> What you say about structs makes sense only as long as structs are
> confined to the stack. In reality, structs can also be on the heap.
> 
> Discussion for bug 4621's has a long discussion about this issue.
> <http://d.puremagic.com/issues/show_bug.cgi?id=4621>

Well, assuming that a struct's destructor is called when it's garbage 
collected from the heap (which as I recall, doesn't ever happen at this 
point), couldn't the GC just catch any exceptions that it throws and then 
throw the appropriate Error? Then the destructor could throw just fine while 
it's on the stack, but you'd get the FinalizerError (or whatever it's called) 
when it happens on the heap.

- Jonathan M Davis


More information about the Digitalmars-d mailing list