Search

(member) functions merged by linker, causing incorrect behavior by wfunc

Closed
as By Design Help for as By Design

1
0
Sign in
to vote
Type: Bug
ID: 775899
Opened: 1/5/2013 11:46:12 PM
Access Restriction: Public
0
Workaround(s)
1
User(s) can reproduce this bug
Visual C++'s linker merges identical functions into a single function during optimization, which affects observable behavior by causing distinct pointer values to become equal, which is a bug.
Details (expand)

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

Visual Studio 2012

Steps to reproduce

#include <iostream>
#include <map>
#include <string>

struct Test
{
    std::map<std::string, void (Test::*)()> m;
    Test()
    {
        this->m["test1"] = &Test::test1;
        this->m["test2"] = &Test::test2;
    }
    void test1() { }
    void test2() { }
    void dispatch(std::string s)
    {
        if (this->m.at(s) == &Test::test1)
        { std::cout << "test1 will be called..." << std::endl; }
        else if (this->m.at(s) == &Test::test2)
        { std::cout << "test2 will be called..." << std::endl; }
        (this->*this->m.at(s))();
    }
};

int main()
{
    Test t;
    t.dispatch("test1");
    t.dispatch("test2");
}

Product Language

English

Operating System

Any

Operating System Language

Any

Actual results

test1 will be called...
test1 will be called...

Expected results

test1 will be called...
test2 will be called...
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 1/8/2013 at 4:46 PM
Hi Mike,

The issue you are seeing is due to an optimization called COMDAT folding which is performed by the linker. This optimization merges identical functions together and is key to reducing code size for C++ code, especially programs which are template heavy. And yes, it is the same issue reported on the link you reference.

You can disable this optimization by using the linker switch "/OPT:NOICF". This will produce the desired behaviour you want but at the cost of some increase in your binary size.

-- Visual C++ Compiler Team
Posted by Microsoft on 1/8/2013 at 12:11 AM
Thank you for submitting feedback on Visual Studio and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by Mike Danes on 1/7/2013 at 7:37 AM
Probably the same as: https://connect.microsoft.com/VisualStudio/feedback/details/716004/merging-equivalent-functions-during-code-generation-leads-to-sub-standard-behavior
Posted by Microsoft on 1/5/2013 at 11:52 PM
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)
Sign in to post a workaround.