Inexplicable invalid memory operation when program terminates

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 5 10:50:59 PDT 2016


On Mon, Sep 05, 2016 at 05:48:11PM +0000, pineapple via Digitalmars-d-learn wrote:
[...]
> In the example the window class destructor was being called. I found
> that if I did `delete win;` at the end there it worked fine, but
> otherwise the GC was tripping up? I removed a console output statement
> and rewrote a thing using a foreach loop instead of a filter range and
> it stopped complaining.
[...]

The problem is caused by trying to allocate memory inside the dtor when
the dtor is invoked by the GC. The delete operator is being deprecated,
but IIRC it doesn't involve a GC collection run, so the problem is
hidden. Basically, class dtors should never do anything involving the
GC, and should not depend on any reference member variables still being
valid.


T

-- 
Never wrestle a pig. You both get covered in mud, and the pig likes it.


More information about the Digitalmars-d-learn mailing list