Search

const data member triggers dynamic initialization with C++ compiler by biochimia

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 716461
Opened: 1/4/2012 3:19:22 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
0
User(s) can reproduce this bug
Below is a reduced test case exemplifying the issue.

    // main.cpp
    #include <assert.h>

    struct Data { int ref; };
    struct ConstData { const Data header; }; // < problematic const

    extern ConstData sharedNull;

    struct File {
        File() { assert(sharedNull.header.ref == -1); }
    };

    File global;
    ConstData sharedNull = { { -1 } };

    int main() {}

Removing the const in the commented line produces the expected results below, but I don't see why that should trigger zero-initialization followed by dynamic-initialization of the global sharedNull.

Changing the const-ness of the global sharedNull variable doesn't affect the results either way. The issue has also been reproduced in optimized builds.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

Visual Studio 2010 SP1

Steps to reproduce

Compile test case and run generated executable:

    > cl main.cpp
    > main.exe

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

C:\dev\static-init>cl main.cpp && main.exe && echo Success!
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

main.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.

/out:main.exe
main.obj
Assertion failed: sharedNull.header.ref == -1, file main.cpp, line 9

C:\dev\static-init>

Expected results

C:\dev\static-init>cl main.cpp && main.exe && echo Success!
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

main.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.

/out:main.exe
main.obj
Success!

C:\dev\static-init>
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 1/5/2012 at 2:37 PM
Hi: while I can reproduce this problem using the Dev10 compiler it appears that it has been fixed in current builds of the compiler. The fix should show up in a future release of Visual C++.

Thanks
Jonathan Caves
Visual C++ Compiler Team
Posted by MS-Moderator07 [Feedback Moderator] on 1/4/2012 at 9:43 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by MS-Moderator01 on 1/4/2012 at 3:42 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Posted by biochimia on 1/4/2012 at 3:20 AM
A vanilla installation of VS 2010 didn't show this issue, but subsequent updates, prior to SP1, do.
Sign in to post a workaround.