Access violation when class method is called via pointer-to-member-function for forward-declared class

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.


Result

The VC++ compiler compiles this without any errors, but the application fails with access violation exception (0xc0000005) during runtime at ‘(rObj.*pFun)();’.

 

Cause

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.


Resolution

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:

Inheritance Keywords (http://msdn2.microsoft.com/en-us/library/ck561bfk.aspx)
#pragma pointers_to_members (http://msdn2.microsoft.com/en-us/library/83cch5a6.aspx)
Representation Method compiler options (/vmb or /vmg) (http://msdn2.microsoft.com/en-us/library/yad46a6z.aspx)


More Information

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)


DISCLAIMER

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

——————————————–

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

 


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image