3
314ccc
I have stumbled upon a very nasty compiler bug. I have boiled it down
to the following code. Granted, this code looks nonsensical, trust
that the original source did much more and solves a specific problem
nicely. The real question here, is why does the compiler compile this
without any problems, yet generates code that crashes. This code
should do nothing...absolutely nothing. There are some comments in
this code that identify places where simple changes can be made to
resolve the crash. I know I can make the crash go away. My question
is what is it doing that is crashing?
File 1: One.h
#pragma once
class Two; // crash
//#include "Two.h" // no crash
class One
{
public:
One(){}
virtual ~One(){}
protected:
Two* m_pTwo;
};
File2: Two.h
#pragma once
#include "Three.h"
class One;
class Two
{
public:
Two();
virtual ~Two();
private:
Three<One> m_cThree;
};
File2 (source): Two.cpp
#include "Two.h"
Two::Two()
{
}
Two::~Two()
{
}
File 3: Three.h
#pragma once
class One;
template<class T> class Three
{
public:
Three();
virtual ~Three();
private:
void (One::*m_pOneMethod)();
};
template<class T> Three<T>::Three()
: m_pOneMethod(0)
{
}
template<class T> Three<T>::~Three()
{
}
File4 (test location):
#include "One.h" // comment out this line fixes crash!?
#include "Two.h"
void CTestDlg::OnBnClickedButton1()
{
Two cTest;
}
to the following code. Granted, this code looks nonsensical, trust
that the original source did much more and solves a specific problem
nicely. The real question here, is why does the compiler compile this
without any problems, yet generates code that crashes. This code
should do nothing...absolutely nothing. There are some comments in
this code that identify places where simple changes can be made to
resolve the crash. I know I can make the crash go away. My question
is what is it doing that is crashing?
File 1: One.h
#pragma once
class Two; // crash
//#include "Two.h" // no crash
class One
{
public:
One(){}
virtual ~One(){}
protected:
Two* m_pTwo;
};
File2: Two.h
#pragma once
#include "Three.h"
class One;
class Two
{
public:
Two();
virtual ~Two();
private:
Three<One> m_cThree;
};
File2 (source): Two.cpp
#include "Two.h"
Two::Two()
{
}
Two::~Two()
{
}
File 3: Three.h
#pragma once
class One;
template<class T> class Three
{
public:
Three();
virtual ~Three();
private:
void (One::*m_pOneMethod)();
};
template<class T> Three<T>::Three()
: m_pOneMethod(0)
{
}
template<class T> Three<T>::~Three()
{
}
File4 (test location):
#include "One.h" // comment out this line fixes crash!?
#include "Two.h"
void CTestDlg::OnBnClickedButton1()
{
Two cTest;
}