Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 948989 - Last Review: February 8, 2008 - Revision: 1.3
Access violation when class method is called via pointer-to-member-function for forward-declared class
Source: Microsoft Support
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.
AÂ method is called through pointer and respective class is only forward declared.
For example:
class TestClass;
void Disp(TestClass& rObj, void (TestClass::*pFun)())
{
   (rObj.*pFun)();
}
Here ‘TestClass’ and function ‘Disp’ are in the same project, but defined in different .CPP files.
The VC++ compiler compiles this without any errors, but the application fails with access violation exception (0xc0000005) during runtime at ‘(rObj.*pFun)();’.
The representation of a pointer-to-member depends on the declaration of the class. It can vary from a simple offset or pointer in the simple case to a complicated structure in the case where virtual functions or multiple inheritance is involved. When you just forward declare like this, the compiler doesn’t know about it and generates code assuming the “simple†representation. At runtime, the value of pFun (which was built by code that
does know it needs to be complicated) isn’t using the same layout and you get a crash.
If possible, #include the header file that contains the whole declaration of the class TestClass (making forward declaration ‘class TestClass;’ unnecessary).
If including the header is not possible (such as cases where a header is not available or when a circular reference would result), you can give the compiler information about the complexity of the class inheritance, so that it can generate the appropriately more "pessimistic" code, using one of the following options:
Pointers to Members
(http://msdn2.microsoft.com/en-us/library/f2wbycwh.aspx)
Pointers to Members and Virtual Functions
(http://msdn2.microsoft.com/en-us/library/fa0207h3.aspx)
MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALSâ€) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.
APPLIES TO
- Microsoft Visual Studio 2005 Express Edition
- Microsoft Visual Studio 2005 Professional Edition
- Microsoft Visual Studio 2005 Service Pack 1
- Microsoft Visual Studio 2005 Standard Edition
- Microsoft Visual Studio 2005 Team Suite
- Microsoft Visual Studio 2008 Academic Edition
- Microsoft Visual Studio 2008 Professional Edition
- Microsoft Visual Studio 2008 Standard Edition
- Microsoft Visual Studio Team System 2008 Team Suite
| kbnomt kbrapidpub KB948989 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate