[Issue 6437] Refcounted calls dtor before ctor, never calls dtor for globals

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 12 12:29:58 PST 2012


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


Ferdinand Majerech <kiithsacmp at gmail.com> changed:

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


--- Comment #1 from Ferdinand Majerech <kiithsacmp at gmail.com> 2012-01-12 12:29:56 PST ---
The destructor call seems to happen in struct 
specialization of emplace(), which is called from
RefCounted._RefCounted.initialize() .

The result variable in emplace() has its destructor called when
it goes out of scope.(In reply to comment #0)

> module globaldtors;
> import std.typecons;
> 
> import std.stdio;
> 
> struct Foo
> {
>     struct Payload
>     {
>         this(int x)
>         {
>             writeln("constructor called");
>         }
> 
>         ~this()
>         {
>             writeln("destructor called");
>         }
> 
>         this(this) { assert(false); }
>         void opAssign(Foo.Payload rhs) { assert(false); }
>     }
> 
>     private alias RefCounted!(Payload, RefCountedAutoInitialize.yes) Data;
>     Data data;
> 
>     this (int x)
>     {
>         data = Data(x);
>     }
> }
> 
> Foo foo;
> 
> void main()
> {
>     /+
>      + destructor called <- this should not happen
>      + constructor called
>      +                    <- where's the dtor call?
>      +/
>     // foo = Foo(1);
> 
> 
>     /+
>      + destructor called <- this should not happen
>      + constructor called
>      + destructor called
>      +/
>     // auto bar = Foo(1);
> }
> 
> Uncomment the two assignments individually to verify they print what's in the
> comments. Regardless if it's a global or not, a dtor is being called first for
> some reason, this looks like a bug.
> 
> But if the variable is also a global (TLS/shared doesn't matter), the dtor is
> not called on application exit.

-- 
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