473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C++ Crashing Template Bug

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;
}

Nov 17 '05 #1
6 1929
31****@gmail.com wrote:
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?


VC++ implements a (strictly speaking non-conforming) optimization which uses
4 different representations for pointers to members. When you declare a
pointer to a member of an incomplete class, the compiler is forced to choose
the most general representation (16 bytes), but when you declare a pointer
to member of a complete class, the compiler will choose the most efficient
representation (in this case, 4 bytes).

As a result, in this code, when you simply forward declare ClassTwo, you end
up with two different representations for pointers to members of ClassTwo,
which ultimately leads to disaster.

You need to compile this code with /vmg, or use #pragma pointers_to_members.
Look them up in MSDN.

-cd
Nov 17 '05 #2
31****@gmail.com wrote:
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?


VC++ implements a (strictly speaking non-conforming) optimization which uses
4 different representations for pointers to members. When you declare a
pointer to a member of an incomplete class, the compiler is forced to choose
the most general representation (16 bytes), but when you declare a pointer
to member of a complete class, the compiler will choose the most efficient
representation (in this case, 4 bytes).

As a result, in this code, when you simply forward declare ClassTwo, you end
up with two different representations for pointers to members of ClassTwo,
which ultimately leads to disaster.

You need to compile this code with /vmg, or use #pragma pointers_to_members.
Look them up in MSDN.

-cd
Nov 17 '05 #3
Thank you for this response! Adding this option takes care of the
problem without any of the mystery that was worrying me.

After reading in the help about the pragma, I am unsure of the scope of
it. If I only want to use it in this instance would I add inside the
class definition?

template<class T> class Three
{
public:
Three();
virtual ~Three();

private:
#pragma pointers_to_members(full_generality)
void (One::*m_pOneMethod)();

};

Nov 17 '05 #4
Thank you for this response! Adding this option takes care of the
problem without any of the mystery that was worrying me.

After reading in the help about the pragma, I am unsure of the scope of
it. If I only want to use it in this instance would I add inside the
class definition?

template<class T> class Three
{
public:
Three();
virtual ~Three();

private:
#pragma pointers_to_members(full_generality)
void (One::*m_pOneMethod)();

};

Nov 17 '05 #5
31****@gmail.com wrote:
Thank you for this response! Adding this option takes care of the
problem without any of the mystery that was worrying me.

After reading in the help about the pragma, I am unsure of the scope
of it. If I only want to use it in this instance would I add inside
the class definition?

template<class T> class Three
{
public:
Three();
virtual ~Three();

private:
#pragma pointers_to_members(full_generality)
void (One::*m_pOneMethod)();

};


No, it applies to a class, not a member, so you'd do

#pragma pointers_to_members(full_generality)
class One;

Once given, the #pragma applies to all subsequent class definitions that
aren't already known to use a different representation, or aren't marked
with one of the inheritance keywords.

In your case, the inheritance keywords (which I neglected to mention in my
earlier reply) are probably the way to go:

// One.h

class __single_inheritance Two;

-cd


Nov 17 '05 #6
31****@gmail.com wrote:
Thank you for this response! Adding this option takes care of the
problem without any of the mystery that was worrying me.

After reading in the help about the pragma, I am unsure of the scope
of it. If I only want to use it in this instance would I add inside
the class definition?

template<class T> class Three
{
public:
Three();
virtual ~Three();

private:
#pragma pointers_to_members(full_generality)
void (One::*m_pOneMethod)();

};


No, it applies to a class, not a member, so you'd do

#pragma pointers_to_members(full_generality)
class One;

Once given, the #pragma applies to all subsequent class definitions that
aren't already known to use a different representation, or aren't marked
with one of the inheritance keywords.

In your case, the inheritance keywords (which I neglected to mention in my
earlier reply) are probably the way to go:

// One.h

class __single_inheritance Two;

-cd


Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Yannick Majoros | last post by:
Hello, PHP is crashing (segfault) due to some bug. See http://bugs.php.net/bug.php?id=32929 for details, but here is the code in which it occurs: while...
0
by: Henry Hank | last post by:
Environment: I'm setting up a database server on a Dell Poweredge 2650, dual 1.8GHZ pentium with 1GB of memory and RAID5 drives. I've installed RedHat 9, and updated the kernel to 2.4.20-19.9smp....
5
by: Eddie | last post by:
I have a MySQL-server running Innodb. We have installed ~ 2GB of memory in the server. In spite of this MySQL keeps crashing due to out-of-memory errors. The server is a dual xeon i686 running...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
0
by: jphelan | last post by:
I have a subform that works fine until you import it into a new database when it crashes if you try to open it in either disign or form view. The form, "Attendees_Subform" in my application was...
0
by: John Phelan Cummings | last post by:
I have a subform that works fine until you import it into a new database when it crashes if you try to open it in either disign or form view. The form, "Attendees_Subform" in my application was...
0
by: 314ccc | last post by:
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...
2
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I've been looking at references about this all afternoon but I can't really see anything that seems relevant to my particular situation. I have a gridview. This is used in read-only mode only,...
2
by: =?Utf-8?B?QW5uZXh4eHh4eHg=?= | last post by:
My computer keeps crashing. The files created when it does this are: WERdc15.dir00\Mini090708-03.dmp WERdc15.dir00\sysdata.xml has anybody any idea of what these files are and the solution to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.