[Issue 4147] Failing inner expression leaves ghost-reference to outer expression on GC which is errenously finalized

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun May 2 18:47:51 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4147


nfxjfg at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg at gmail.com


--- Comment #1 from nfxjfg at gmail.com 2010-05-02 18:47:49 PDT ---
Here is a somewhat reduced test case:
>>>>
class Parent {
    this( Object c ) { assert(false, "a"); }
    ~this() { assert( false, "b" ); }
}

Object foo() { throw new Exception( "Some error" ); }

void main() {
    try {
        auto parent = new Parent( foo() );
    } catch (Exception e) {
    }
}
<<<<

This fails with assertion b (the dtor), even though the constructor (assertion
a) was never called.

Disassembly proves that a new object is allocated, before foo() and the ctor of
Parent is called. Possible solutions:
- allow the runtime to run the dtor/finalizer only if the ctor was called (set
a flag immediately before calling the ctor or so)
- allocate the object only immediately before calling the ctor

I'm not sure if Walter would agree that this is a bug. The dtor gets called
even if the ctor fails by throwing an exception. In general, the user has to be
very careful with finalizers: using finalizers is full of synchronization
issues, trouble caused by not being allowed to access references, and so on.
Basically a programmer's trap.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list