Microsoft KB Archive/325141

= FIX: Assert on Olefact.cpp Line 52 with IMPLEMENT_DYNCREATE and IMPLEMENT_OLECREATE_EX =

Article ID: 325141

Article Last Modified on 4/11/2003

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q325141



SYMPTOMS
When using IMPLEMENT_DYNCREATE and IMPLEMENT_OLECREATE_EX, you receive the following error message:

---

Microsoft Visual C++ Debug Library

---

Debug Assertion Failed!

Program: c:\projects\debug\Repro.exe

File: olefact.cpp

Line: 52

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

---

Abort Retry Ignore

---



CAUSE
The Visual C++ .NET compiler does not respect the order of declaration when __declspec(selectany) is used.



RESOLUTION
In the Stdafx.h header file of the project that consumes your Microsoft Foundation Classes (MFC) extension DLL, predefine an empty AFX_COMDAT symbol by pasting the following code before you include (#include) any other header files: #define AFX_COMDAT



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft Visual C++ .NET (2003).



MORE INFORMATION
The problem is that IMPLEMENT_RUNTIMECLASS uses the symbol AFX_COMDAT, as shown in the following code: AFX_COMDAT is expanded by the preprocessor to __declspec(selectany).
 * 1) define IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, wSchema, pfnNew, class_init)   AFX_COMDAT const CRuntimeClass class_name::class##class_name = {

IMPLEMENT_OLECREATE_EX does not use __declspec(selectany) (or AFX_COMDAT).

Steps to Reproduce the Behavior
There are two ways to reproduce the behavior:
 * Method 1 demonstrates just the core issue.
 * Method 2 demonstrates the problem in an MFC context and demonstrates how to generate Debug Assertion Failure.

Method 1
int func(char c) { return printf(&quot;%c\n&quot;,c); } int a = func('a'); __declspec(selectany) int b = func('b'); int c = func('c'); int main { return a+b; } // Expected result: a b c // Actual result: a c b
 * 1) include 

Method 2 (in Visual C++ .NET)
 Create an MFC extension DLL:  Use the MFC DLL template to create a new Visual Studio .NET project. On the Application Settings page of the wizard, under DLL type, click MFC extension DLL, and then click Finish to create the DLL.   Work with the classes as follows:  Create a new MFC class named CBase that is derived from CWnd:  In ClassView, right-click the project, click Add, and then click Add Class.</li> In the Add Class wizard, select the MFC Class template, click Open, type CBase in the Class name box, and then click Finish.</li></ol> </li> Export (and then import) the whole class from the DLL by marking the class with the AFX_EXT_CLASS macro.</li> Paste the following DECLARE_DYNAMIC macro anywhere in the base class (if it has not already been added by the code wizards):</li></ol>

class AFX_EXT_CLASS CBase : public CWnd {   DECLARE_DYNAMIC(CBase) ... }                   </li> Create a new MFC executable project to test the new extension DLL. This can be of type Win32 Console Project (with MFC support added), or it can be the standard MDI or SDI MFC applications.</li>  In the new MFC executable project, open the Stdafx.h file, go to the end of the file, and then paste in this reference to the header file: #include <afxctl.h>                   </li> In the MFC executable, create a class named CDerived that is derived from the CBase class (which is exported from the extension DLL):  In ClassView, right-click the project, click Add, and then click Add Class.</li> In the Add Class wizard, select the MFC Class template, and then click Open.</li> Type CDerived in the Class name box, and then click Finish.</li></ol> </li> Change the following as described: <ul> <li>Replace all references to CWnd in the new class with CBase.</li> <li>In derived.h, change the base class for CDerived from CWnd to CBase.</li> <li>In derived.cpp, change the IMPLEMENT_DYNAMIC and BEGIN_MESSAGE_MAP macros to use CDerived instead of CWnd.</li></ul> </li> <li>You must #include the header file for CBase: in derived.cpp, add #include  where   includes the path to the file Base.h.</li> <li>Link with the import library created for the extension DLL: <ol style="list-style-type: lower-alpha;"> <li>In Solution Explorer, right-click the project, and then click Properties.</li> <li>In the left pane, expand the Linker folder, and then click Input.</li> <li>In the right pane, in the Additional Dependencies box, type  where   is the name (including the path) of the export library that is generated by the extension DLL project that you created earlier.</li></ol> </li> <li> In the CDerived class definition, replace the macro DECLARE_DYNAMIC call with the macro DECLARE_DYNCREATE, and then add the following macro call to DECLARE_OLECREATE_EX: class CDerived : public CBase {   DECLARE_DYNCREATE(CDerived) DECLARE_OLECREATE_EX(CDerived); ... }                   </li> <li>On the Tools menu, click Create GUID, use format 1 (IMPLEMENT_OLECREATE), and then fill in the Xs in the IMPLEMENT_OLECREATE_EX macro shown in the next step.</li> <li>In the implementation file for CDerived, replace IMPLEMENT_DYNAMIC with IMPLEMENT_DYNCREATE.</li> <li> In the implementation file for CDerived, paste the following code anywhere in global space: IMPLEMENT_OLECREATE_EX(CDerived, &quot;Derived&quot;, xxxxxxxx, xxxx, xxxx, xx, xx, xx, xx, xx, xx, xx, xx); BOOL CDerived::CDerivedFactory::UpdateRegistry(BOOL) {return FALSE;} </li> <li>Build the MFC extension DLL project, and then build the MFC executable project. Make sure that the DLL can be found from the executable file.</li> <li>Run the MFC executable. The Debug Assertion Failed error message occurs.</li></ol>

Keywords: kbbug kbfix kbpending kbcompiler KB325141

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.