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

6.5k Views Asked by At

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.

4

There are 4 best solutions below

0
Anton On

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

1
Petr Vepřek On

I had a similar fatal error while building a trivial C++ command line app and using Microsoft Visual Studio Community 2019, version 16.6.2.

Changing the default /LTCG:incremental to /LTCG in project -> properties -> linker -> optimization for release configuration fixed the problem.

Just to add, this error started after one of the recent VS2019 updates (not sure which specific one).

0
Theo On

I resolved this issue to link big library in 64-bit. The Visual Studio tries with Default (32-bit linking).

Configuration Properties -> Advanced -> Preferred Build Tool Architecture -> 64-bit (x64)

0
shlomtzi On

The error C1001 related to compiler file p0io.c is mainly caused by enabling "Use Unicode UTF-8" for worldwide language supporting Region Settings. You could see the link about the concrete reason. You could click

Time &Language -> Region-> Additional date, time, &regional settings-> Region-> Administrative->Change system locale

and turn off UTF-8.