INFO: Multiple Instance Large Model Windows MFC Applications

ID: Q85736


The information in this article applies to:


SUMMARY

In the small and medium memory models, all objects based on the Microsoft Foundation Class library must be placed in the near heap. This imposes a limit on the number of objects that can be allocated because the near heap is typically less than 64K in size. The Microsoft Foundation Class library does not allow a small or medium model application to create a far instance of any of the standard classes from the Microsoft Foundation Class library. To create far instances of classes based on the Microsoft Foundation Class library, the compact or large memory model must be used.

This article discusses some of the issues regarding large memory model Microsoft Windows-based applications built with the Microsoft Foundation Class library.


MORE INFORMATION

Member functions of classes are called with an implicit "this" pointer, which points to the data for the class. By default, the size of the this pointer is determined by the ambient memory model. For small and medium models, the this pointer is near. For large, compact, and huge models, the this pointer is far. For example, consider the following line of code in a medium memory model application:


   lpMyString = new FAR CString; 
The pointer to the new object will be a far pointer. However, the member functions of the class CString in the medium memory model Microsoft Foundation Class library expect a near this pointer. As a result, the compiler will generate a C2662 error stating that it cannot convert the this pointer from far to near. For more information on the this pointer and how it is affected by different memory models, please refer to the on-line help for the __far keyword.

An application must use the compact or large memory model to allocate far instances of Microsoft Foundation Class derived classes. To use these memory models, the appropriate libraries must be built. Please see the README.TXT file in the MFC\SRC directory for instructions on building different memory model versions of the Microsoft Foundation Class library.

Compact and large memory model Windows-based applications raise special issues. Windows can run multiple instances of an application only if the application has a single data segment (DGROUP). Multiple instances of a compact or large memory model application can be run only if all of the application's static and global data is located in DGROUP. To help enable this, build the Microsoft Foundation Class libraries and applications by using the /Gx compiler switch.

By default, in the compact and large memory models, the compiler allocates initialized data items as near if they are smaller than or equal in size to the threshold value set by the /Gt option. The /Gx switch extends this initialized data allocation rule to data that is un-initialized and data that is marked as extern. The /Gx option does not affect the size of pointers. Pointers remain far by default. For more information on the /Gt and /Gx compiler switches, please refer to the on-line help for the command- line options for the C/C++ compiler.

When building the Microsoft Foundation Class libraries, use the OPT=/Gx option on the NMAKE command line. Instances of classes created using new can then be allocated outside of DGROUP, overcoming the 64K limit.

The command line to build a large model Windows version of the Microsoft Foundation Classes library with the /Gx option is:

   NMAKE MODEL=L TARGET=W OPT=/Gx 
NOTE: The /Gx option does not work for static or global objects. These objects must be explicitly declared as NEAR in order for them to be placed in DGROUP. For example, in the HELLO.CPP sample Microsoft Foundation Class Windows-based application, the following code used to create the application object

   CTheApp theApp; 
should be changed to the following:

   CTheApp NEAR theApp; 

Additional query words:


Keywords          : kb16bitonly kbnokeyword kbMFC kbVC 
Version           : 1.0 1.5 1.51 1.52 7.0
Platform          : 
Issue type        : kbinfo 

Last Reviewed: July 30, 1999