Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ fatal error C1001: An internal error has occurred in the compiler

I'm getting the following error when compiling in release mode.

1>d:\users\eyal\projects\code\yalla\core\src\runbox\win32\window.cpp : fatal error C1001: An internal error has occurred in the compiler.
1>         (compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 249)
1>          To work around this problem, try simplifying or changing the program near the locations listed above.
1>         Please choose the Technical Support command on the Visual C++
1>          Help menu, or open the Technical Support help file for more information
1>           link!RaiseException()+0x48
1>           link!CxxThrowException()+0x65
1>           link!std::_Xout_of_range()+0x1f
1>           link!InvokeCompilerPass()+0x1b4e2
1>           link!InvokeCompilerPass()+0x22efe
1>           link!InvokeCompilerPass()+0x2332e
1>           link!InvokeCompilerPass()+0x232f9
1>           link!InvokeCompilerPass()+0x233cb
1>           link!InvokeCompilerPass()+0x22b04
1>           link!InvokeCompilerPass()+0x22d86
1>           link!DllGetC2Telemetry()+0x115837
1>
1>     1>
1>LINK : fatal error LNK1257: code generation failed

I'm using VS2015 Update 2 RC.

I'm not sure but maybe it's a bug in the optimizer?

The code that causes it is as follow:

window.h

class Window {
public:
    Window();
    ~Window();
    void show();
    void hide();
private:
    class NativeControl;
    std::unique_ptr<NativeControl> _window;
};

window.cpp

class Window::NativeControl : public NativeWindow {
public:
    NativeControl() : NativeWindow() { }
};

Window::Window()
    : _window(std::make_unique<Window::NativeControl>()) {
}

Window::~Window() {
}

void Window::show() 
{
    _window->show(WindowShowMode::Show);
}

void Window::hide()
{
    _window->show(WindowShowMode::Hide);
}

NativeWindow is the native Window of whatever OS.

Here is a working code compiled with GCC 5.1: https://ideone.com/4YvjRK

Just to make a note.

If I'll remove the inheritance and replace it with something like this.

class Window::NativeControl {
public:
    void show(WindowShowMode showMode)
    {
    }
};

It will work fine!

Here is the same code compiled with GCC 5.1 with no inheritance: https://ideone.com/Mu0A42

What seems to cause this behaviour is the derivation of NativeControl from NativeWindow.

The steps to reproduce it as as follow:

  1. Remove the dtor declaration and definitions from the Window class.
  2. Try to Build (not Rebuild).
  3. The compiler will complain and give you bunch of errors.

1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\memory(1194): error C2338: can't delete an incomplete type 1> 1> 1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\memory(1195): warning C4150: deletion of pointer to incomplete type 'Yalla::Window::NativeControl'; no destructor called 1>
d:\Users\Eyal\Projects\Code\Yalla\core\src\runbox\include\window.h(13): note: see declaration of 'Yalla::Window::NativeControl' 1>
window.cpp 1> 1>Build FAILED.

  1. Add back the dtor to the Window class.
  2. Build again (not Rebuild).
  3. At this point the compiler should complain with the following error "fatal error C1001: An internal error has occurred in the compiler."

The interesting part is that doing rebuild seems to fix the problem!

What I want to achieve is basically have the actual implementation of NativeWindow in a different file, mostly for simplicity and not so much about reusability.

I guess that instead of doing that in inheritance that maybe confuses the unique_ptr template I can also do that through composition and expose the instance of NativeWindow through a getter and it might work but the question is whether there are better ways to do it?

I'm relearning C++ after a very long time I didn't touch it so if some of the things I'm doing don't make sense, please tell me about it!

Update:

The C++ standard says:

The template parameter T of unique_ptr may be an incomplete type.

I found a post about it in Herb Sutter's blog.

like image 723
Eyal Alon Avatar asked Mar 15 '16 07:03

Eyal Alon


1 Answers

similar error with

(compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 255)

was fixed changing Properties->Linker->Optimization->Link Time Code Generation from /LTCG:incremental to /LTCG

Studio 2015 Update 3

like image 195
Anton Avatar answered Oct 27 '22 21:10

Anton