Mihajlo Cvetanović
2010-02-03 12:17:04 UTC
I made a minimal VS2008 project that compiles cleanly in Debug build but
reports Run-Time Check Failure #0 during execution. The problem is that
Data class is defined in one place and just declared in another. It
seems that sizeof(pointer-to-member) is different in these two cases. If
the order of includes is changed in Test.cpp OR in Configuration.cpp OR
there is #include "Data.h" instead of struct Data; in Configuration.h
then there is no stack corruption. Is this a known bug (or a bug at all)?
=== Test.cpp ============================
#include "stdafx.h"
#include "Data.h"
#include "Configuration.h"
Configuration global_object(&Data::member);
int _tmain(int argc, _TCHAR* argv[])
{
Data data;
data.member = 42;
int member = data.*(global_object.member_pointer);
return 0;
}
=========================================
=== Configuration.cpp ===================
#include "stdafx.h"
#include "Configuration.h"
#include "Data.h"
Configuration::Configuration(int Data::* member_pointer) :
member_pointer(member_pointer)
{
}
=========================================
=== Configuration.h =====================
struct Data;
class Configuration
{
public:
Configuration(int Data::* member_pointer);
int Data::* member_pointer;
};
=========================================
=== Data.h ==============================
struct Data
{
int member;
};
=========================================
reports Run-Time Check Failure #0 during execution. The problem is that
Data class is defined in one place and just declared in another. It
seems that sizeof(pointer-to-member) is different in these two cases. If
the order of includes is changed in Test.cpp OR in Configuration.cpp OR
there is #include "Data.h" instead of struct Data; in Configuration.h
then there is no stack corruption. Is this a known bug (or a bug at all)?
=== Test.cpp ============================
#include "stdafx.h"
#include "Data.h"
#include "Configuration.h"
Configuration global_object(&Data::member);
int _tmain(int argc, _TCHAR* argv[])
{
Data data;
data.member = 42;
int member = data.*(global_object.member_pointer);
return 0;
}
=========================================
=== Configuration.cpp ===================
#include "stdafx.h"
#include "Configuration.h"
#include "Data.h"
Configuration::Configuration(int Data::* member_pointer) :
member_pointer(member_pointer)
{
}
=========================================
=== Configuration.h =====================
struct Data;
class Configuration
{
public:
Configuration(int Data::* member_pointer);
int Data::* member_pointer;
};
=========================================
=== Data.h ==============================
struct Data
{
int member;
};
=========================================