www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8923] New: Destructors of struct members get called at the wrong point

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923

           Summary: Destructors of struct members get called at the wrong
                    point
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: malteskarupke web.de



PDT ---
The destructor of the member of a struct doesn't get called when I expect it to
be called. I'd expect it to be called at the end of the destructor of the
containing struct. I'm not sure when it gets called instead, but it is too
late. Here is a case that segfaults in DMD 2.060:

void main()
{
    bool destructorCalled = false;
    struct DestructorCounter
    {
        ~this()
        {
            destructorCalled = true; // segmentation fault
        }
    }
    struct S
    {
        DestructorCounter a;
    }
    S s;
}

If this is supposed to be invalid I can provide examples that are most
definitely not supposed to be invalid, but are a bit more lengthy.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 31 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru



---
It seems that dmd doesn't pass allocated value correctly. In this case it emits
in main function code:

callq  <_d_allocmemory>
movq   $0x0,(%rax)
movb   $0x0,0x8(%rax)
lea    -0x8(%rbp),%rax
xor    %rcx,%rcx
mov    %rcx,(%rax)
mov    %rax,%rdi

So, allocated value is overwritten and destructor gets incorrect pointer in
%rdi. However, if the case is simplified to one level (replace S s; with
DestructorCounter s;) dmd emits correct code:

callq  <_d_allocmemory>
movq   $0x0,(%rax)
movb   $0x0,0x8(%rax)
mov    %rax,-0x8(%rbp)
lea    -0x8(%rbp),%rax
xor    %rcx,%rcx
mov    %rax,%rdi

Now %rdi points to allocated memory. Probably, because S field destructor
doesn't access destructorCalled object forces dmd to pass invalid value.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 31 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code



In Windows, s1's dtor causes Access Violation, but s2's dtor runs successfully.

import core.stdc.stdio : printf;
void main()
{
    bool destructorCalled = false;
    struct DestructorCounter
    {
        ~this()
        {
            printf("-dtor\n");
            destructorCalled = true; // segmentation fault
        }
    }
    struct S
    {
        DestructorCounter a;
    }
    S s1;       // Access Violation
    S s2 = S(); // OK
}

Output:
---
-dtor            (s2's dtor call)
-dtor            (s1's dtor call)
object.Error: Access Violation
---

I think this is definitely a wrong-code bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 31 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923




PDT ---
I debugged the issue I was having further, and it is definitely a different
problem than I thought it was. It does not have to do with destructors being
called in the wrong order, and actually the test case from the first post isn't
showing the problem that I encountered.

It was just that once I had that test case I felt like I should submit it. I'll
try to figure out a test case for my real problem, but I'll do that in a
different bug report.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 31 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923




Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/585f5a3a3db9f856dc4904910b8a705969b89d33
fix Issue 8923 - Destructors of struct members get called at the wrong point

https://github.com/D-Programming-Language/phobos/commit/f76ea73eff85d260f6b1642dbada9701dc3a7fa6


Supplemental fix for Issue 8923

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923




Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/704230b135f3e4b25470b4522927fe97a43604c1
fix Issue 8923 - Destructors of struct members get called at the wrong point

If a default-initialized variable declaration requies some frame pointers
for it's field, they should be initialized by the StructLiteralExp to fill
the hidden field.

https://github.com/D-Programming-Language/dmd/commit/e1c9119d2a622ecf4bfc3b5c34f20a33f7f4a2c1


Issue 8923 - Destructors of struct members get called at the wrong point

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923


Denis Shelomovskij <verylonglogin.reg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |verylonglogin.reg gmail.com
            Summary|Destructors of struct       |Nested structs have null
                   |members get called at the   |context pointers in static
                   |wrong point                 |array variables and struct
                   |                            |fields



18:30:23 MSK ---
As commits are already pushed referencing this one, renaming the issue and
providing a comprehensive example (instead of creating a new one one marking
this as a duplicate):
---
void main()
{
    int i;
    struct CS { void f() { ++i; } }
    struct S { CS cs; }

    CS cs;
    assert(cs.tupleof[$-1] != null); // ok

    CS[1] csArr;
    assert(csArr[0].tupleof[$-1] != null); // fails

    S s1 = S();
    assert(s1.cs.tupleof[$-1] != null); // ok

    S s2;
    assert(s2.cs.tupleof[$-1] != null); // fails
}
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 08 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923


Denis Shelomovskij <verylonglogin.reg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com



18:32:53 MSK ---
*** Issue 8951 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 08 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923




18:33:29 MSK ---
*** Issue 8952 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 08 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8923




18:42:35 MSK ---
Please, when you are fixing issues with such incorrect descriptions, create a
new one and mark this as a duplicate or rename this one to have a correct title
in commit message. Thanks.

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