473,489 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1933
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
1829
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
1454
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
12887
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
2705
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
1832
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
1377
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
245
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
1965
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
1860
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
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7142
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,...
1
6847
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
7352
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...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.