SmartHeapϊ

Getting Started and
Platform Guide

for

Windows NT Intel
Windows NT Alpha
Windows 95

 

 

 

 

 

 

 

 

 

 

Version 5.0


SmartHeap is a trademark and HeapAgent is a registered trademark of Compuware Corporation.

Microsoft, Windows, and Win32 are registered trademarks and Visual C++, Win95, and Windows NT are trademarks of Microsoft Corporation.Κ

All other trademarks are the property of their respective holders.

Copyright © 1994-1999 Compuware Corporation.
All rights reserved.

Printed March 17, 1998

 

 

 

 

 

 

 

 

 

 

 

 

 

Weβd like to have your comments about this manual because your suggestions help us improve our products.Κ Please send comments to:

MicroQuill Software Publishing, Inc.
11200 Kirkland Way, Suite 310
Kirkland, Washington Κ98033

Voice:ΚΚΚΚΚΚΚΚ (425) 827-7200
Fax:ΚΚΚΚΚΚΚΚΚΚΚΚ (425) 481-4442
Internet:ΚΚΚΚ info@microquill.com


Contents

New in SmartHeap 5.0............................................................................. ΚΚΚΚΚΚΚΚΚ 1

New small-block allocator........................................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 1

Reduced space overhead........................................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 1

New API to control process heap growth............................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 1

Installing SmartHeap................................................................................ ΚΚΚΚΚΚΚΚΚ 2

Files in the Win32 SmartHeap release...................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 2

Using SmartHeap with Microsoft Visual C++..................................... ΚΚΚΚΚΚΚΚΚ 5

Quick start..................................................................................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 5

Specifying the location of SmartHeap files...................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 5

Setting up a Visual C++ MFC project............................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 6

Setting up a Visual C++ non-MFC project....................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 7

Setting up your application for the SmartHeap runtime libraryΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 8

Adding the SmartHeap header file to your source files................. ΚΚΚΚΚΚΚΚΚΚΚΚΚ 8

Specifying the location of SmartHeap files...................................... ΚΚΚΚΚΚΚΚΚΚΚΚΚ 9

Setting up the Visual C++ project file............................................. ΚΚΚΚΚΚΚΚΚΚΚ 10

Debugging an application with SmartHeap........................................... ΚΚΚΚΚΚΚΚΚΚΚ 12

Adding the Debug SmartHeap header file
to your source files........................................................................ ΚΚΚΚΚΚΚΚΚΚΚ 12

Specifying the location of Debug SmartHeap files....................... ΚΚΚΚΚΚΚΚΚΚΚ 13

Setting up the Visual C++ project file............................................. ΚΚΚΚΚΚΚΚΚΚΚ 14

Recompiling or relinking your application..................................... ΚΚΚΚΚΚΚΚΚΚΚ 16

SmartHeap/Microsoft Visual C++ compatibility issues....................... ΚΚΚΚΚΚΚΚΚΚΚ 17

Resolving Visual C++ duplicate-definition linker errors.............. ΚΚΚΚΚΚΚΚΚΚΚ 17

Using SmartHeap with Visual C++ 4.x
Debug C runtime libraries............................................................. ΚΚΚΚΚΚΚΚΚΚΚ 17

Using SmartHeap with MFC 3.0/4.x................................................ ΚΚΚΚΚΚΚΚΚΚΚ 18

Compiler functions that SmartHeap overrides.............................. ΚΚΚΚΚΚΚΚΚΚΚ 19


Using SmartHeap with Borland C++.................................................. ΚΚΚΚΚΚΚ 20

Getting started with the Runtime SmartHeap library............................ ΚΚΚΚΚΚΚΚΚΚΚ 20

Adding the SmartHeap header file to your source files............... ΚΚΚΚΚΚΚΚΚΚΚ 20

Setting up the Borland C++ project file.......................................... ΚΚΚΚΚΚΚΚΚΚΚ 21

Debugging an application with SmartHeap........................................... ΚΚΚΚΚΚΚΚΚΚΚ 22

Adding the Debug SmartHeap header file
to your source files........................................................................ ΚΚΚΚΚΚΚΚΚΚΚ 22

Setting up the Borland C++ project file.............................. ΚΚΚΚΚΚΚΚΚΚΚ 23

Recompiling your application.......................................................... ΚΚΚΚΚΚΚΚΚΚΚ 24

SmartHeap/Borland C++ compatibility issues....................................... ΚΚΚΚΚΚΚΚΚΚΚ 25

Using SmartHeap with OWL............................................................ ΚΚΚΚΚΚΚΚΚΚΚ 25

SmartHeap Win32 platform notes...................................................... ΚΚΚΚΚΚΚ 26

Multi-threading in Win32......................................................................... ΚΚΚΚΚΚΚΚΚΚΚ 26

Using shared memory in SmartHeap for Win32.................................... ΚΚΚΚΚΚΚΚΚΚΚ 26

SmartHeapβs automatic DLL patching.................................................... ΚΚΚΚΚΚΚΚΚΚΚ 30

First-chance exceptions reported by your debugger........................... ΚΚΚΚΚΚΚΚΚΚΚ 35

SmartHeap for Win32 calling convention.............................................. ΚΚΚΚΚΚΚΚΚΚΚ 35

SmartHeap Win32φspecific values......................................................... ΚΚΚΚΚΚΚΚΚΚΚ 36

Default error handling in Win32.............................................................. ΚΚΚΚΚΚΚΚΚΚΚ 37


New in SmartHeap 5.0

New small-block allocator

An improved small-block allocator incurs zero bytes per allocation of overhead and recycles space more effectively between block sizes than the SmartHeap 3.x/4.x small-block allocator.Κ If you want to use the old small-block allocator for some reason, you may use MemPoolSetSmallBlockAllocator to change the small-block allocator for a given memory pool.Κ Use MemPoolSetSmallBlockAllocator(MemDefaultPool, MEM_SMALL_BLOCK_SH3) to use the SH 3.0/4.0 small-block allocator.Κ Specify the value MEM_SMALL_BLOCK_SH5 for the new small-block allocator.

Reduced space overhead

In addition to the more space-efficient small-block allocator, SmartHeap 5.0 no longer creates multiple default memory pools for multi-threaded applications, so multiple threads are able to share the same pages of heap memory in a contention-free manner rather than using separate memory pools.

New API to control process heap growth

SmartHeap 5.0 has a new API, MemProcessSetGrowIncrement, that allows you to control how much memory SmartHeap requests from the operating system when a memory pool needs to grow. The memory returned is retained in SmartHeapβs free pool and is allocated to any memory pool(s) that subsequently need to grow.Κ By buffering operating system heap requests, SmartHeap 5.0 incurs less system call overhead, less operating system heap and address space fragmentation, and less operating system heap contention.

The default process grow increment is 2MB.Κ The value you specify to MemProcessSetGrowIncrement is in bytes and is rounded up to the next 64K.

Installing SmartHeap

The files on the SmartHeap installation disks are not compressed or copy protected.Κ

       To install SmartHeap on your hard disk:

1.ΚΚ Copy the files in the include directory to a directory on your compilerβs include file path.

2.ΚΚ Copy the files in the library directory corresponding to your compiler (msvc) to a directory on your linkerβs library file path.

3.ΚΚ Copy the files in the bin directory to the Windows NT system32 or Windows 95 system directory, or some other directory on the PATH.

4.ΚΚ (optional)Κ Copy the files from source and/or samples to examine or modify the SmartHeap source files and sample applications.

Files in the Win32 SmartHeap release

The Win32 version of SmartHeap includes:

αΚΚΚΚΚ Import libraries for Microsoft Visual C++.

αΚΚΚΚΚ Statically linkable libraries for Microsoft Visual C++.

αΚΚΚΚΚ Compiler-independent DLLs.

αΚΚΚΚΚ Platform-independent header files.

αΚΚΚΚΚ Platform-independent source files for the malloc and C++ operator new definitions.


The SmartHeap diskette contains the following Win32φspecific files:

DirectoryΚΚΚΚΚΚΚΚΚ FileΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Description

includeΚΚΚΚ smrtheap.hΚΚΚ Header file containing all Runtime SmartHeap C declarations except malloc.

includeΚΚΚΚ smrtheap.hppΚ Header file containing Runtime SmartHeap C++ declarations, including operator new.

includeΚΚΚΚ shmalloc.hΚΚΚ Header file containing declarations for Runtime SmartHeap ANSI C functions (malloc, etc.).

includeΚΚΚΚ heapagnt.hΚΚΚ Header file containing all Debug SmartHeap declarations.

binΚΚΚΚΚΚΚΚ shw32.dllΚΚΚΚ Runtime SmartHeap dynamic link library.

binΚΚΚΚΚΚΚΚ shw32.dbgΚΚΚΚ Symbols for Runtime SmartHeap DLL.

binΚΚΚΚΚΚΚΚ ha312w32.dllΚ Debug SmartHeap dynamic link library.

msvcΚΚΚΚΚΚΚ smrtheap.cΚΚΚ Source file for γquick startδ procedure when using SmartHeap with a non-MFC application..

msvcΚΚΚΚΚΚΚ shmfc.cppΚΚΚΚ Source file for γquick startδ procedure when using SmartHeap with an MFC application..

msvcΚΚΚΚΚΚΚ shlw32m.libΚΚ Runtime SmartHeap single-thread statically linkable library for Microsoft Visual C++.

msvcΚΚΚΚΚΚΚ shdw32m.libΚΚ Runtime SmartHeap single-thread DLL import library for Microsoft Visual C++.

msvcΚΚΚΚΚΚΚ shlw32mt.libΚΚΚ Runtime SmartHeap multi-thread statically linkable library for Microsoft Visual C++.

msvcΚΚΚΚΚΚΚ shdw32mt.libΚΚΚ Runtime SmartHeap multi-thread DLL import library for Microsoft Visual C++.

msvcΚΚΚΚΚΚΚ shmfc4m.libΚΚ Statically linkable single-thread library file for Microsoft Visual C++/MFC 4.x/5.x χ for statically linked Release MFC only; must link before SmartHeap library.

msvcΚΚΚΚΚΚΚ shmfc4mt.libΚ Statically linkable multi-thread library file for Microsoft Visual C++/MFC 4.x/5.x χ for statically linked Release MFC only; must link before SmartHeap library.


DirectoryΚΚΚΚΚΚΚΚΚ FileΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Description

msvcΚΚΚΚΚΚΚ haw32m.libΚΚΚΚΚΚΚΚ Debug SmartHeap multi-thread DLL import library for Microsoft Visual C++.

msvcΚΚΚΚΚΚΚ hamfc32m.libΚ Statically linkable library file for Microsoft Visual C++ 2.x/MFC 3.0 χ for statically linked _DEBUG MFC only; must link before Debug SmartHeap library.

msvcΚΚΚΚΚΚΚ hamfc4m.libΚΚ Statically linkable library file for Microsoft Visual C++/MFC 4.x/5.x χ for statically linked _DEBUG MFC only; must link before Debug SmartHeap library.

borlandΚΚΚΚ shdw32b.libΚΚ Runtime SmartHeap single-thread DLL import library for Borland C++ (Intel only).

borlandΚΚΚΚ shdw32bt.libΚ Runtime SmartHeap multi-thread DLL import library for Borland C++ (Intel only).

borlandΚΚΚΚ haw32b.libΚΚΚ Debug SmartHeap multi-thread DLL import library for Borland C++ (Intel only).

sourceΚΚΚΚΚ shmalloc.cΚΚΚ SmartHeap definition for ANSI C (malloc, etc.) functions χ provided so you can customize these.

sourceΚΚΚΚΚ shmalmac.cΚΚΚ Macro versions of SmartHeap ANSI C (malloc, etc.) functions.

sourceΚΚΚΚΚ shnew.cppΚΚΚΚ SmartHeap definition for operator new χ provided so you can customize and/or build a library for non-supported compilers.

sourceΚΚΚΚΚ shnewhnd.cppΚ SmartHeap new handler definitions

sourceΚΚΚΚΚ def*.c,ΚΚΚΚΚΚ Default memory pool definitions

ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ db*.cΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ used by the above malloc and new definitions.

samplesΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Various sample SmartHeap applications with Microsoft Visual C++ and Borland C++ make files.


Using SmartHeap with Microsoft Visual C++

If youβre developing an application with Microsoft Visual C++ for 32-bit Windows, hereβs how to get started with SmartHeap.Κ

Quick start

If your goal is to quickly get SmartHeap into your project so that it simply replaces malloc and operator new in your application, follow the instructions in this section.Κ This procedure uses a source file with #pragma statements that force references to the correct set of SmartHeap libraries for your application based on your projectβs compile-time flags.Κ This method is better than explicitly referencing SmartHeap libraries, because it ensures that the correct SmartHeap libraries are linked φ both initially and whenever you subsequently change your project settings.

If you want to use SmartHeap APIs, or if you want explicit control of which SmartHeap libraries your application uses, see the detailed instructions in the sections γSetting up your application for the SmartHeap runtime libraryδ and γDebugging an application with SmartHeap,δ later in this booklet.

Specifying the location of SmartHeap files

       To tell Visual C++ where to find SmartHeap include and library files:

1.ΚΚ From the Visual C++ Tools menu, choose Options, and the Options dialog box appears.

2.ΚΚ Choose the Directories tab.Κ

3.ΚΚ Under Show Directories For, choose γInclude filesδ.


4.ΚΚ Choose the Add button, and add the path of the SmartHeap include directory to the include path list, for example:Κ

ΚΚΚΚ c:\smrtheap\include

5.ΚΚ Choose the Add button again, and add the path of the SmartHeap msvc directory to the include path list, for example:Κ

ΚΚΚΚ c:\smrtheap\msvc

6.ΚΚ Under Show Directories For, choose γLibrary filesδ.

7.ΚΚ Choose the Add button, and add the path of the SmartHeap msvc directory to the library path list, for example:Κ

ΚΚΚΚ c:\smrtheap\msvc

8.ΚΚ Choose OK to save your changes.

Setting up a Visual C++ MFC project

       To set up a Visual C++ MFC project to work with SmartHeap:

1.ΚΚ Open the file stdafx.cpp in your project.

2.ΚΚ At the beginning of stdafx.cpp, before any #include statements, insert the following text:

ΚΚΚΚ #include "shmfc.cpp"

3.ΚΚ Save your changes to stdafx.cpp.

4.ΚΚ Choose Rebuild All from the Project menu (Visual C++ 2.x) or from the Build menu (Visual C++ 4.x/5.x).

5.ΚΚ Repeat the Rebuild All command for both the Release and Debug project configurations.


Setting up a Visual C++ non-MFC project

       To set up a Visual C++ project to work with SmartHeap if the project does not use MFC:

1.ΚΚΚ Open your applicationβs project file.

2.ΚΚΚ Add the file smartheap.c to your project.Κ This file is located in the SmartHeap msvc directory.Κ

3.ΚΚΚ Display the Project Settings dialog box.

4.ΚΚΚ Under Settings For, choose γWin32 Releaseδ, if it isnβt chosen already.

5.ΚΚΚ Choose the Link tab.

6.ΚΚΚ At the beginning of γObject/library modulesδ, before any other object files or libraries, enter the following:

ΚΚΚΚ .\Release\smrtheap.obj

7.ΚΚ Under Settings For, choose γWin32 Debugδ.

8.ΚΚ Choose the Link tab.

9.ΚΚ At the beginning of γObject/library modulesδ, before any other object files or libraries, enter the following:

ΚΚΚΚ .\Debug\smrtheap.obj

10.Κ Choose OK to save changes to Project Settings.Κ

11.Κ Rebuild both Release and Debug versions of the project.


Setting up your application for the SmartHeap runtime library

Hereβs the detailed procedure for setting up your application to use the SmartHeap runtime library.Κ For information on debugging your application with Debug SmartHeap, see γDebugging an application with SmartHeap,δ later in this booklet.

Adding the SmartHeap header file to your source files

Important!Κ If youβre not calling SmartHeap APIs, skip to the next page.Κ You donβt need to add SmartHeap header files if you simply want SmartHeap to replace malloc and operator new.Κ

       For each file that makes SmartHeap API calls:

¬ΚΚΚ For C source files, include smrtheap.h, which contains declarations for the C SmartHeap APIs.

ΚΚΚΚΚΚ #include <smrtheap.h>

¬ΚΚΚ For C++ source files, include smrtheap.hpp, which contains declarations for SmartHeap versions of operator new and which itself includes smrtheap.h.

ΚΚΚΚΚΚ #include <smrtheap.hpp>

In both cases, the #include directive must appear after any compiler or library header files that declare malloc (for example, stdlib.h) or new (for example, new.h or afx.h).


Specifying the location of SmartHeap files

       To tell Visual C++ where to find SmartHeap include and library files:

1.ΚΚ Choose Options from the Visual C++ Tools menu, and the Options dialog box appears.

2.ΚΚ Choose the Directories tab.Κ

3.ΚΚ Under Show Directories For, choose γInclude files.δ

4.ΚΚ Choose the Add button, and add the path of the SmartHeap include directory to the include path list, for example:Κ

ΚΚΚΚΚ c:\smrtheap\include

5.ΚΚ Again under Show Directories For, choose γLibrary files.δ

6.ΚΚ Choose the Add button, and add the path of the SmartHeap msvc directory to the library path list, for example:Κ

ΚΚΚΚΚ c:\smrtheap\msvc

7.ΚΚ Choose OK to save your changes.


Setting up the Visual C++ project file

Hereβs how you set up a Visual C++ project file to work with SmartHeap.Κ

NoteΚ You must repeat the following procedure for each EXE and DLL in your application.Κ

       To set up a Visual C++ project to work with SmartHeap:

1.ΚΚ Choose Open Workspace from the Visual C++ File menu, and open your applicationβs project file.

2.ΚΚ Choose Settings from the Project menu (Visual C++ 2.x) or Build menu (Visual C++ 4.x/5.x), and the Project Settings dialog box appears.

3.ΚΚ Under Settings For, choose γWin32 Release,δ if it isnβt chosen already.

4.ΚΚ Choose the Link tab.

5.ΚΚ Specify the SmartHeap libraries that you want to link with.Κ At the beginning of Object/Library Modules, before any Visual C++ libraries, enter the appropriate libraries:

ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Link with these libraries
If youβre using the SmartHeap DLL withΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ (in the order shown)

An EXE or DLL in a single-threaded app,
without MFC, with statically linked MFC 3.0,
or the MFC 3.0/4.x DLLΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shdw32m.lib

An EXE or DLL in a multi-threaded app,
without MFC, with statically linked MFC 3.0,
or the MFC 3.0/4.x DLLΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shdw32mt.lib

An EXE or DLL with a single-threaded app,
with statically-linked MFC 4.xΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shmfc4m.lib, shdw32m.lib

An EXE or DLL with a multi-threaded app,
with statically-linked MFC 4.xΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shmfc4mt.lib, shdw32mt.lib


ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Link with these libraries
If youβre statically linking SmartHeap withΚΚΚΚΚΚΚΚΚΚΚ (in the order shown)

An EXE or DLL in a singled-threaded app,
without MFC, or with statically-linked MFC 3.0ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shlw32m.lib

An EXE or DLL in a multi-threaded app,
without MFC, or with statically-linked MFC 3.0ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shlw32mt.lib

An EXE or DLL in a single-threaded app,
with statically-linked MFC 4.xΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shmfc4m.lib, shlw32m.lib

An EXE or DLL in a multi-threaded app,
with statically-linked MFC 4.xΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ shmfc4mt.lib, shlw32mt.lib

NoteΚ The above library files appear in the msvc sub-directory under the directory where you installed SmartHeap.Κ

MFC users!Κ If your application uses the DLL version of MFC, you must use the DLL version of SmartHeap (shdw32m.lib).Κ If your application links with Debug MFC, you must link with Debug SmartHeap.

Caution!Κ If your application has more than one thread, you must link with the multi-thread SmartHeap 4.0 libraries.Κ In previous versions of SmartHeap, all libraries were thread-reentrant as long as the MEM_POOL_SERIALIZE flag was specified.Κ In SmartHeap 4.0, only libraries whose names end it Ξtβ are thread-reentrant.

6.ΚΚ Choose OK to save changes to Project Settings.Κ

7.ΚΚ Choose Build filename from the Project menu (Visual C++ 2.x) or Build menu (Visual C++ 4.x/5.x), and Visual C++ relinks the application.


Debugging an application with SmartHeap

Hereβs the detailed procedure for setting up your application for debugging with Debug SmartHeap.Κ

Adding the Debug SmartHeap header file to your source files

If you want to call Debug SmartHeap APIs (dbgMemXXX), youβll need to include the Debug SmartHeap header file and recompile your application.Κ

NoteΚ Simply linking with the Debug SmartHeap Library is sufficient to allow SmartHeap to perform full error detection.Κ Moreover, if your application is compiled with Visual C++ PDB debugging information, Debug SmartHeap obtains file names and line numbers directly from the debugging information.Κ So if you arenβt calling Debug SmartHeap APIs, there is no need to recompile your application for Debug SmartHeap.

Alpha users!Κ For non-Intel platforms, Debug SmartHeap is not able to obtain file and line information from Visual C++ PDB debug information.Κ Therefore, if youβre using the Alpha version of SmartHeap and if you want Debug SmartHeap to include file names and line numbers in error reports, you must include heapagnt.h in your source files and define the preprocessor constants MEM_DEBUG and DEFINE_NEW_MACRO.

       For each file from which you call Debug SmartHeap APIs:

¬ΚΚΚ Include heapagnt.h, which contains declarations of memory-allocation functions both for ANSI C (malloc, free, and so on) and for C++ (including the new and delete operators).Κ The #include directive must appear after any compiler or library header files that declare malloc (for example, stdlib.h) or new (for example, new.h or afx.h):

ΚΚΚΚΚΚ #include <heapagnt.h>

NoteΚ If you already include smrtheap.h, shmalloc.h, or smrtheap.hpp, then you donβt need to also include heapagnt.h.Κ The Runtime SmartHeap header files automatically include heapagnt.h when MEM_DEBUG is defined.

Specifying the location of Debug SmartHeap files

       To tell Visual C++ where to find Debug SmartHeap include and library files:

1.ΚΚ Choose Options from the Visual C++ Tools menu, and the Options dialog box appears.

2.ΚΚ Choose the Directories tab.Κ

3.ΚΚ Under Show Directories For, choose γInclude files.δ

4.ΚΚ Choose the Add button, and add the path of the Debug SmartHeap include directory to the include path list, for example:Κ

ΚΚΚΚ c:\smrtheap\include

5.ΚΚ Again under Show Directories For, choose γLibrary files.δ

6.ΚΚ Choose the Add button, and add the path of the Debug SmartHeap msvc directory to the library path list, for example:Κ

ΚΚΚΚ c:\smrtheap\msvc

7.ΚΚ Choose OK to save your changes.


Setting up the Visual C++ project file

Hereβs how you set up a Visual C++ project file so you can debug the corresponding EXE or DLL with Debug SmartHeap.Κ

NoteΚ You must repeat the following procedure for each EXE and DLL in your application.Κ

       To set up a Visual C++ project to work with Debug SmartHeap:

1.ΚΚ Choose Open Workspace from the Visual C++ File menu, and open your applicationβs project file.

2.ΚΚ Choose Settings from the Project menu (Visual C++ 2.x) or Build menu (Visual C++ 4.x/5.x), and the Project Settings dialog box appears.

3.ΚΚ Under Settings For, choose γWin32 Debug,δ if it isnβt chosen already.

4.ΚΚ Choose the C/C++ tab and, under Debug Info, choose γProgram Database.δ

5.ΚΚ (required for Alpha and PowerPC, optional otherwise)Κ If youβre using Intel SmartHeap, and if your application doesnβt call Debug SmartHeap APIs, you can skip this step.

Still at the C/C++ tab, in Preprocessor Definitions, add:

ΚΚΚΚ MEM_DEBUG=1

6.ΚΚ (Alpha and PowerPC only)Κ Also in Preprocessor Definitions, add:

ΚΚΚΚ DEFINE_NEW_MACRO=1

7.ΚΚ Choose the Link tab.

8.ΚΚ Check the Generate Debug Info checkbox.


9.ΚΚ Still at the link tab, based on the following table, specify the Debug SmartHeap libraries that you want to link with.Κ At the beginning of Object/Library Modules, before the Visual C++ libraries, enter the appropriate libraries:

ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ Link with these libraries
If youβre debuggingΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ (in the order shown)

An EXE or DLL without MFC, with non-Debug MFC,
or with the Debug MFC DLLΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ haw32m.lib

An EXE or DLL with Debug MFC 3.0 statically linkedΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ hamfc32m.lib,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ haw32m.lib

An EXE or DLL with Debug MFC 4.x statically linkedΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ hamfc4m.lib,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ haw32m.lib

NoteΚ The files haw32m.lib, hamfc32m.lib, and hamfc4m.lib appear in the msvc sub-directory under the directory where you installed SmartHeap.Κ

10.Κ Choose OK to save changes to Project Settings.Κ


Recompiling or relinking your application

If youβre calling Debug SmartHeap APIs, or if youβre using the Alpha version of SmartHeap, you need to include the Debug SmartHeap header file and recompile your application.Κ Information on including this header file appears at the beginning of the section.

NoteΚ You only need to recompile your application if youβve included the Debug SmartHeap header file in one or more of your source files.Κ However, you must always relink each EXE and DLL in your application for Debug SmartHeap.

       To recompile your application:

1.ΚΚ From the File menu, choose Open Workspace, and open your applicationβs project file, if you havenβt already done so.

2.ΚΚ Choose Rebuild All from the Project menu (Visual C++ 2.x) or from the Build menu (Visual C++ 4.x/5.x), and Visual C++ compiles the application.

       To relink your application:

1.ΚΚ From the File menu, choose Open Workspace, and open your applicationβs project file, if you havenβt already done so.

2.ΚΚ Choose Build filename from the Project menu (Visual C++ 2.x) or from the Build menu (Visual C++ 4.x/5.x), and Visual C++ relinks the application.


SmartHeap/Microsoft Visual C++ compatibility issues

Resolving Visual C++ duplicate-definition linker errors

If your application does not reference the functions malloc and free, you may receive duplicate-definition linker errors when you try to link the SmartHeap library into your application.Κ To eliminate these linker errors, use the following procedure:

1.ΚΚΚ From the File menu, choose Open Workspace, and open your applicationβs project file, if you havenβt already done so.

2.ΚΚΚ Choose Settings from the Project menu (Visual C++ 2.x) or from the Build menu (Visual C++ 4.x/5.x), and the Project Settings dialog box appears.

3.ΚΚΚ Choose the Link tab, and select the Input category.

4.ΚΚΚ In Force Symbol References, add the symbol _SmartHeap_malloc.

Alternatively, if youβre using the command-line linker, add the following linker option:

-include:_SmartHeap_malloc

Using SmartHeap with Visual C++ 4.x Debug C runtime libraries

Visual C++ 4.0 and higher includes debugging versions of the C runtime libraries.Κ Debug SmartHeap supports these libraries, but Runtime SmartHeap does not.Κ Therefore, if your application links with the Debug C runtime library, it must also link with the Debug SmartHeap library (haw32m.lib) rather than the Runtime SmartHeap library (shlw32m[t].lib or shdw32m[t].lib).


Debug SmartHeap overrides the Visual C++ functions _calloc_dbg, _realloc_dbg, _free_dbg, _msize_dbg, _expand_dbg, _CrtCheckMemory, _CrtDumpMemoryLeaks, _CrtIsValidHeapPointer, _CrtIsMemoryBlock, _CrtSetAllocHook, and _CrtSetBreakAlloc.Κ Debug SmartHeap emulates the behavior of each of these Visual C++ routines, so you can use these routines interchangeably with Debug SmartHeap APIs.

Using SmartHeap with MFC 3.0/4.x

Debug SmartHeap defines replacements for all of the public memory-debugging APIs of the Microsoft Foundation Class Library.Κ This allows Debug SmartHeap to be used with _DEBUG MFC.Κ Runtime SmartHeap is not compatible with Debug MFC.Κ Debug SmartHeap emulates all MFC APIs except for AfxDoForAllObjects and the CMemoryState member functions.Κ For the unsupported APIs, SmartHeap defines stubs that print error messages that point to the corresponding SmartHeap APIs offering similar functionality.

If your application uses the statically linked Debug version of MFC, you must link with the SmartHeap/MFC integration library:Κ hamfc32m.lib for MFC 3, or hamfc4m.lib for MFC 4.x.Κ Be sure to place this library before either the SmartHeap or MFC libraries on the linker command line.Κ If your application uses the statically linked Release version of MFC 4.x, you must link with shmfc4m.lib.Κ Note that no integration is required for the Release (non-debug) version of MFC 3.0, since this library doesnβt override new/delete.

The DLL version of MFC 3.0 and higher requires applications to share the same memory manager that the MFC DLL itself uses.Κ (This is a change from MFC 2.x, where the MFC DLL called back to the client application for memory management.)Κ To allow SmartHeap applications to work with the DLL version of MFC, the SmartHeap DLLs automatically patch the memory-management functions in the MFC and CRT DLLs.Κ

ImportantΚ The statically linked SmartHeap library (shlw32m.lib) does not patch MFC or CRT DLLs, so if your application uses the MFC or CRT DLLs, you must use the DLL version of SmartHeap (shdw32m.lib or haw32m.lib).


Compiler functions that SmartHeap overrides

SmartHeap overrides malloc, calloc, realloc, free, new, and delete on all platforms, as explained in €2.3.2, €2.3.3, and €2.3.4 of the SmartHeap Programmerβs Guide.Κ In addition, the Microsoft Visual C++ SmartHeap libraries override the following functions:

FunctionΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ SmartHeap library module

_expandΚΚΚΚΚΚΚ shmalloc

_heapchkΚΚΚΚΚΚ shmalloc

_heapminΚΚΚΚΚΚ shmalloc

_heapsetΚΚΚΚΚΚ shmalloc

_heapwalkΚΚΚΚΚ shmalloc

_msizeΚΚΚΚΚΚΚΚ shmalloc

If you donβt want SmartHeap to override these C runtime functions, you should remove the applicable object module from the SmartHeap library that you link with.Κ For example, use the following command to prevent SmartHeap from overriding malloc with the statically linked Microsoft Runtime SmartHeap library:

lib shlw32m.lib /remove:shmalloc.obj

If you remove the shmalloc object module from a SmartHeap library, you will need to define SmartHeap_malloc at file scope.Κ For example:

int SmartHeap_malloc = 0;


Using SmartHeap with Borland C++

If youβre developing an application with Borland C++ version 4.x or 5.x for 32-bit Windows, hereβs how to get started with SmartHeap.Κ

Getting started with the Runtime SmartHeap library

Hereβs how you set up your application to use the Runtime SmartHeap library.Κ For information on debugging your application with Debug SmartHeap, see γDebugging an application with SmartHeap,δ later in this booklet.

Adding the SmartHeap header file to your source files

Important!Κ If youβre not calling SmartHeap APIs, skip to the next section.Κ You donβt need to add SmartHeap header files if you simply want SmartHeap to replace malloc and operator new.Κ

       For each file that makes SmartHeap API calls:

¬ΚΚΚ For C source files, include smrtheap.h, which contains declarations for the C SmartHeap APIs.

ΚΚΚΚΚΚ #include <smrtheap.h>

¬ΚΚΚ For C++ source files, include smrtheap.hpp, which contains declarations for SmartHeap versions of operator new and which itself includes smrtheap.h.

ΚΚΚΚΚΚ #include <smrtheap.hpp>

In both cases, the #include directive must appear after any compiler or library header files that declare malloc (for example, stdlib.h) or new (for example, new.h or afx.h).

Setting up the Borland C++ project file

Hereβs how you set up a Borland C++ project file to work with Runtime SmartHeap.

NoteΚ You must repeat the following procedure for each EXE and DLL in your application.Κ

       To set up a Borland C++ project to work with Runtime SmartHeap:

1.ΚΚ Choose Open Project from the Borland C++ Project menu, and open your .ide file.

2.ΚΚ Choose Project from the Options menu, and the Project Options dialog box appears.

3.ΚΚ From the Topics list, choose Directories (if it isnβt already chosen), and the directories options appear.

4.ΚΚ In the Include list, add the path of the SmartHeap include directory, for example:Κ

ΚΚΚΚΚΚΚ c:\smrtheap\include

5.ΚΚ In the Library list, add the path of the SmartHeap borland directory, for example:Κ

ΚΚΚΚΚΚΚ c:\smrtheap\borland

6.ΚΚ Choose OK to save your changes.

7.ΚΚ Add the Runtime SmartHeap Libraries that are pertinent to your application.Κ To add each library, select your projectβs EXE or DLL in the Project window, press the Insert key, and specify the file in the Add to Project List dialog box:

αΚΚΚΚΚ If youβre using SmartHeap with a single-threaded application, link with shdw32b.lib.

αΚΚΚΚΚ If youβre using SmartHeap with a multi-threaded application, link with shdw32bt.lib.

These files appear in the borland sub-directory under the directory where you installed SmartHeap.Κ


OWL users!Κ If your application uses the DLL version of OWL, you must use the DLL version of SmartHeap (shdw32b.lib or shdw32bt.lib).

8.ΚΚ Choose Make All from the Project menu, and Borland C++ links the application.

Debugging an application with SmartHeap

Hereβs how you set up your application for debugging with Debug SmartHeap.Κ

Adding the Debug SmartHeap header file to your source files

If you want Debug SmartHeap to include source file and line information in its error reports, or if you want to call Debug SmartHeap APIs (dbgMemXXX), you need to include the Debug SmartHeap header file and recompile your application.Κ

NoteΚ Simply linking with the Debug SmartHeap Library is sufficient to allow SmartHeap to perform full error detection, but in most cases youβll also want to recompile your application so that SmartHeap includes source file names and line numbers in its error reports.

       For each file for which you want SmartHeap to report source file and line information:

¬ΚΚΚ Include heapagnt.h, which contains declarations of memory-allocation functions both for ANSI C (malloc, free, and so on) and for C++ (including the new and delete operators).Κ The #include directive must appear after any compiler or library header files that declare malloc (for example, stdlib.h) or new (for example, new.h or afx.h):

ΚΚΚΚΚΚ #include <heapagnt.h>


NoteΚ If you already include smrtheap.h, shmalloc.h, or smrtheap.hpp, then you donβt need to also include heapagnt.h.Κ The Runtime SmartHeap header files automatically include heapagnt.h when MEM_DEBUG is defined.

Setting up the Borland C++ project file

Hereβs how you set up a Borland C++ project file to work with Debug SmartHeap.

NoteΚ You must repeat the following procedure for each EXE and DLL in your application.Κ

       To set up a Borland C++ project to work with Debug SmartHeap:

1.ΚΚ Choose Open Project from the Borland C++ Project menu, and open your .ide file.

2.ΚΚ Choose Project from the Options menu, and the Project Options dialog box appears.

3.ΚΚ From the Topics list, choose Directories (if it isnβt already chosen), and the directories options appear.

4.ΚΚ In the Include list, add the path of the SmartHeap include directory, for example:Κ

ΚΚΚΚΚΚΚ c:\smrtheap\include

5.ΚΚ In the Library list, add the path of the SmartHeap borland directory, for example:Κ

ΚΚΚΚΚΚΚ c:\smrtheap\borland

6.ΚΚ Now double-click Compiler in the Topics list, and the list expands to show the compiler options.

7.ΚΚ Choose Defines from the list, and enter this value in Defines:

ΚΚΚΚΚΚΚ MEM_DEBUG=1
DEFINE_NEW_MACRO=1

8.ΚΚ Choose OK to save your changes.


9.ΚΚ Add the Debug SmartHeap library, haw32b.lib.Κ Select your projectβs EXE or DLL in the Project window, press the Insert key, and specify the file in the Add to Project List dialog box:

This file appears in the borland sub-directory under the directory where you installed SmartHeap.Κ

Recompiling your application

If you want SmartHeap to include source file and line number information in its error reports, you need to include the Debug SmartHeap header file and recompile your application.Κ Information on including this header file appears at the beginning of the section.

NoteΚ You only need to recompile your application if youβve included the Debug SmartHeap header file in one or more of your source files.Κ However, you must always relink each EXE and DLL in your application for SmartHeap.

       To recompile your application:

1.ΚΚ Choose Open Project from the Project menu, and open the .ide file, if you havenβt already done so.

2.ΚΚ Choose Build All from the Project menu, and Borland C++ compiles the application.

       Or, to relink your application:

1.ΚΚ Choose Open Project from the Project menu, and open the .ide file, if you havenβt already done so.

2.ΚΚ Choose Make All from the Project menu, and Borland C++ links the application.


SmartHeap/Borland C++ compatibility issues

Using SmartHeap with OWL

The DLL version of the Borland Object Windows Library (OWL) requires applications to share the same memory manager that the OWL DLL itself uses. To allow SmartHeap applications to work with the DLL version of OWL, the SmartHeap DLLs automatically patch the memory-management functions in the OWL and CRT DLLs.Κ


SmartHeap Win32 platform notes

Multi-threading in Win32

SmartHeap 4.0 and higher include both single- and multi-thread versions of its non-debug libraries, plus a multi-thread debug library.Κ Only the multi-thread libraries are thread reentrant.Κ If your application creates multiple threads, you must link with the multi-thread SmartHeap library φ one with a name ending in Ξtβ.

NoteΚ It is not necessary to call MemRegisterTask or define MemPoolDefaultFlags in SmartHeap 4.0 and higher as it in previous SmartHeap versions and on some other platforms.Κ However, if you create your own SmartHeap memory pools, you must still specify MEM_POOL_SERIALIZE in the flags parameter if the pool is referenced in more than one thread.

Using shared memory in SmartHeap for Win32

Beginning in version 3.1, SmartHeap supports Windows 95 and Windows NT shared memory.Κ SmartHeap uses memory mapped files to implement shared memory pools.Κ All of the SmartHeap allocation APIs that accept a memory pool parameter, including MemAllocPtr, MemAllocFS, MemAlloc, and operator new with the placement syntax, support Win32 shared memory.

To create shared memory pools in Win95 or NT, use MemPoolInitNamedShared or MemPoolInitNamedSharedEx.Κ You must specify both the name and size parameters as non-zero.Κ

αΚΚΚΚΚ The name parameter is passed through to CreateFileMapping.Κ It can contain any characters but backslash (\).Κ

αΚΚΚΚΚ The size parameter is used to reserve address space for the memory pool.Κ The memory pool cannot grow larger than this size, but SmartHeap will commit memory to the shared pool based on allocation requests.Κ Therefore, you should specify a size that is the maximum (high water mark) memory requirement for the pool.Κ SmartHeap will commit only as much physical memory as you allocate from the pool regardless of the size parameter you specify.Κ Nevertheless, you should not specify an excessively large size parameter, or you will waste address space.Κ This is particularly important in Win95, where shared memory address space is common to all processes.Κ To help in choosing a suitable size value, you can use MemPoolSize to determine the total memory actually consumed by a memory pool at any time.

Once youβve created a shared memory pool, you can call MemPoolAttachShared from other processes, specifying the same name you previously supplied to MemPoolInitNamedShared[Ex].Κ If you donβt know which of your processes will create and which will attach to a shared pool, you can call MemPoolInitNamedShared[Ex] from each process, specifying the same name in each call.Κ If a shared pool of the given name already exists, MemPoolInitNamedShared[Ex] will attach to and return that pool.Κ In this case, the other parameters to MemPoolInitNamedShared[Ex] are ignored.

Mapping shared memory pools to the same address in each process

Because SmartHeap allocation APIs return direct pointers to memory, SmartHeap requires shared memory pools to be mapped to the same address in each process.Κ In Windows 95, this is not a problem since shared memory is always mapped to the same address in each process.Κ NT, however, does not guarantee that shared memory is mapped to the same address in each process.Κ If you create a shared pool in one process and call MemPoolAttachShared in another process in which the address of the memory pool is unavailable, MemPoolAttachShared will fail (it will return NULL after reporting a MEM_OUT_OF_MEMORY error).

To solve this problem, SmartHeap 3.1 includes a new API, MemPoolInitNamedSharedEx.Κ This API lets you specify the address at which a shared memory pool should be mapped (addr) and/or an array of process IDs that will access the shared pool (pids).Κ If you specify a non-NULL value for pids, SmartHeap will search the address space of each of these processes to find a suitable address that is available in all of the processes.

MemPoolInitNamedSharedEx also lets you specify security Win32 attributes for the shared pool.Κ The security parameter you specify is passed through to CreateFileMapping.

In choosing the addr parameter, you should examine the DLLs in each process that will use the shared pool, and find an address space location that is available in each process.Κ You should use MemPoolAttachShared as early as possible during process initialization to ensure that the address range you selected is not used by a call to VirtualAlloc.

If you specify addr as NULL, SmartHeap chooses a random address in the upper half of the application address space for NT, or a random address in the shared memory address space for Win95.Κ This minimizes the chance of collisions with other shared pools or VirtualAlloc objects (which are normally allocated from the beginning of the address space).

If all of the processes that will share a memory pool are running at the time you create the memory pool, you can have SmartHeap find an address automatically by specifying pidCount and pids parameters.Κ In this case, the shared pool will be mapped into each processβs address space before the MemPoolInitNamedSharedEx call returns.Κ If there is no address space region of suitable size available in every process, MemPoolInitNamedSharedEx will fail (this would be very unusual considering that each process has 2 GB of address space).

If youβve specified a pids array to MemPoolInitNamedSharedEx, and if youβre running on NT, and if youβre using the SmartHeap DLL, then when you call MemPoolAttachShared from one of the processes that you previously specified to MemPoolInitNamedSharedEx, you may specify the return value of MemPoolInitNamedSharedEx as the pool parameter along with a NULL name parameter.Κ This is the only case where the name parameter of MemPoolAttachShared is optional in the Win32 SmartHeap implementation.Κ You may also specify both name and pool parameters as non-NULL, or just the name parameter as non-NULL if you wish.Κ For Win95 compatibility, the name parameter must always be specified as non-NULL.

CautionΚ The address-space searching feature of MemPoolInitNamedSharedEx is implemented fully only in the DLL version of SmartHeap (both Debug and Runtime).Κ In the DLL version (shdw32m.lib), MemPoolInitNamedSharedEx will cause the new pool to be mapped into each specified process before the call returns, which guarantees that the pool can be shared between the given processes.Κ In the statically linked version of SmartHeap (shlw32m.lib), MemPoolInitNamedSharedEx searches each specified process and reserves space at a suitable address region, but does not actually map the pool into the other processes during the call to MemPoolInitNamedSharedEx.Κ If the chosen address region becomes unavailable in a given process before MemPoolAttachShared is called in that process, then MemPoolAttachShared will fail.Κ Therefore, to guarantee that a set of processes will be able to successfully share a memory pool in NT, you must use the DLL version of SmartHeap (shdw32m[t].lib).

Miscellaneous details

Shared memory pools are always serialized with inter-process synchronization mutual exclusion.Κ You can concurrently allocate blocks from a shared pool from multiple processes with complete safety.

You can use all of the SmartHeap allocation and de-allocation APIs with shared memory pools.Κ In Debug SmartHeap, shared memory pools fully support all of the same debugging facilities as private memory pools.

If SmartHeap detects an error such an overwrite in a shared pool, the error is reported to the process in which the error is detected, regardless of where the shared memory pool was created.Κ Debug SmartHeap error reports in Win32 include the ID of both the thread and the process where the error wasΚ detected, as well as the thread/process IDs where the erroneous object was created.

In Debug SmartHeap, when you compile your source files with MEM_DEBUG, SmartHeap stores a pointer to the source file name in each allocated memory block.Κ The actual file name is a string constant, generated by the compiler, that exists only in the private address space of the process.Κ For shared memory pools, SmartHeap copies file name strings into space owned by the shared memory pool, so the file names are accessible from all processes that attach to the pool.Κ Only the first 19 characters of file names are stored, so if you allocate shared memory from source files having longer names, the file names reported in memory error reports will be truncated to the first 19 characters.

To end access to a shared pool from a given process, call MemPoolFree from that process.Κ The shared pool will continue to exist until every process that has access to it calls MemPoolFree.Κ The process that creates a shared pool does not need to be the last process to call MemPoolFree; shared pools are not owned by a particular process.

See also €2.4.5, γShared memory,δ and the entries for MemPoolInitRegion, MemPoolInitNamedShared, MemPoolInitNamedSharedEx, and MemPoolAttachShared in €4.2, γFunction reference,δ in the SmartHeap Programmerβs Guide.

SmartHeapβs automatic DLL patching

NoteΚ You can skip this section if you statically link SmartHeap.Κ Only the SmartHeap DLL does any patching of DLLs.

When you link with the DLL version of SmartHeap, SmartHeap automatically patches heap routines in compiler DLLs, such as the C runtime and MFC DLLs.Κ If youβre using MFC, OWL, or other DLLs that share heap memory with their client EXE, this patching is required to ensure that objects allocated in these DLLs can be safely freed by your applicationβs EXE.

NoteΚ SmartHeap γpatchingδ does not affect the on-disk copy of any DLLs, and it does not affect other applications that use the DLLs that SmartHeap patches.Κ SmartHeap modifies only the in-memory copy of the DLL that is private to your applicationβs process.Κ Runtime SmartHeap patches only those DLLs in your process that export heap APIs such as malloc or new.Κ Debug SmartHeap also patches all DLLs that have PDB debug information and that contain statically linked heap API definitions.

In some cases, you may want to disable SmartHeapβs patching of compiler DLLs.Κ You can selectively or globally disable SmartHeapβs patching by adding values to the registry or by adding an API definition to your EXE or DLL.Κ These two techniques are described in the following two sections.


Controlling patching via the registry

You can disable patching either for all of the DLLs in your application or for selected DLLs by adding keys to the Windows NT or Windows 95 registry, as described in the following procedures.Κ You can add registry keys in either of two ways:

αΚΚΚΚΚ Use the Registry Editor (regedt32.exe on NT, regedit.exe on Win95) to add these registry entries manually.

αΚΚΚΚΚ Use the Win32 registry API to add registry keys and values programmatically.Κ This is the method you should use if your application uses the Runtime SmartHeap DLL and you donβt want SmartHeap to patch one or more of the DLLs used by your application.

Important!Κ If youβre using MFC, OWL, or other DLLs that share heap memory with their client EXE, do not disable patching for those DLLs, or you may encounter incompatibilities and unexpected GPFs.

NoteΚ SmartHeap stores its settings under the HKEY_LOCAL_MACHINE registry key for compatibility with NT services.Κ Previous SmartHeap releases (version 3.01 and earlier) stored these settings in HKEY_CURRENT_USER.

       To disable SmartHeap patching for all DLLs:

1.ΚΚ Add the following key to the Windows NT or Windows 95 registry:

For Runtime SmartHeap, add the registry key:

HKEY_LOCAL_MACHINE\Software\MicroQuill\
SmartHeap\Apps\
<app-name>

For Debug SmartHeap, add the registry key:

HKEY_LOCAL_MACHINE\Software\MicroQuill\
HeapAgent\Apps\
<app-name>

where <app-name> is the full path of your applicationβs EXE.

Important!Κ Use forward slashes (/) as directory delimiters.Κ Backslash characters (\) are not legal in registry keys.Κ

For example, if your applicationβs full path is c:\apps\foo.exe and you want to disable patching for Runtime SmartHeap, youβd add the registry key:

HKEY_LOCAL_MACHINE\Software\MicroQuill\
SmartHeap\Apps\c:/apps/foo.exe

2.ΚΚ At the Runtime SmartHeap or Debug SmartHeap registry key that you added in step 1, add an entry with the following values:

αΚΚΚΚΚ Value name:Κ PatchProcessOn

αΚΚΚΚΚ Data type:Κ REG_DWORD

αΚΚΚΚΚ Value:Κ 0 (zero).Κ Zero means SmartHeap should not patch DLLs, and a non-zero value means that SmartHeap should patch DLLs.

       To disable patching in certain DLLs in your application but not in the entire process:

1.ΚΚ Add the following key and value to the Windows NT or Windows 95 registry:

For Runtime SmartHeap, add the registry key:

HKEY_LOCAL_MACHINE\Software\MicroQuill\
SmartHeap\Apps\
<app-name>\SkipDLLs

For Debug SmartHeap, add the registry key:

HKEY_LOCAL_MACHINE\Software\MicroQuill\
HeapAgent\Apps\
<app-name>\SkipDLLs

Alternatively, if youβre using Debug SmartHeap with more than one application, or if youβre running your application from more than one directory, you can use the following registry key to have Debug SmartHeap skip DLLs in all applications you use with Debug SmartHeap:

HKEY_LOCAL_MACHINE\Software\MicroQuill\
HeapAgent\SkipDLLs


2.ΚΚ At the Runtime SmartHeap or Debug SmartHeap registry key that you added in step 1, add DLLs that you do not want SmartHeap to patch.Κ For each DLL you want SmartHeap to skip (that is, not patch), add an entry with the following values:

αΚΚΚΚΚ Value name:Κ File<n> where <n> is 0 (zero) for the first DLL you add, 1 (one) for the second, and so on

αΚΚΚΚΚ Data type:Κ REG_SZ

αΚΚΚΚΚ Value:Κ the DLL name (not the full path of the DLL χ just file name and extension)

3.ΚΚ To the same registry key, add another entry with the following values:

αΚΚΚΚΚ Value name:Κ FileCount

αΚΚΚΚΚ Data type:Κ REG_DWORD

αΚΚΚΚΚ Value:Κ the number of DLLs youβve added to the registry key

For example, if you want to skip patching DLLs foo.dll and bar.dll, youβd add the following three registry values to the Runtime SmartHeap or Debug SmartHeap registry, as appropriate:

αΚΚΚΚΚ Name File0, type REG_SZ, value foo.dll

αΚΚΚΚΚ Name File1, type REG_SZ, value bar.dll

αΚΚΚΚΚ Name FileCount, type REG_DWORD, value 2

Optionally, the SmartHeap DLL can patch calls to the system heap APIs LocalAlloc, GlobalAlloc, and HeapAlloc with faster SmartHeap equivalents.Κ These APIs are replaced on Windows NT only, not on Windows 95. Beginning in SmartHeap 4.0, patching of system heap APIs is disabled by default.


       To enable SmartHeap patching of system heap APIs:

1.ΚΚ Add the Runtime SmartHeap or Debug SmartHeap registry key as described in step 1 of γTo disable SmartHeap patching for all DLLsδ above.

2.ΚΚΚ At the registry key that you added in step 1, add an entry with the following values:

αΚΚΚΚΚ Value name:Κ PatchSystemAllocsOn

αΚΚΚΚΚ Data type:Κ REG_DWORD

αΚΚΚΚΚ Value:Κ 1 (one).Κ A non-zero value means SmartHeap shouldΚ patch system heap APIs, and a zero value means that SmartHeap should not patch system APIs.

When you load a Debug SmartHeap application and Debug SmartHeap encounters DLLs it does not recognize and that do not have either PDB debugging information or exports of heap APIs, Debug SmartHeap will prompt you with the following message:

HeapAgent is unable to find memory allocation function symbols in module <unknown.dll>. Do you want to add <unknown.dll> to the list of DLLs HeapAgent skips in subsequent loads (choose γYesδ only if this DLL does not share the heap with your EXE or other DLLs)?

If you choose the Yes button, Debug SmartHeap adds the DLL name to the registry entry HKEY_LOCAL_MACHINE\Software\
MicroQuill\HeapAgent\SkipDLLs
, so this DLL will be skipped whenever Debug SmartHeap encounters it in any application.


       To trace SmartHeap patching activity to a log file:

1.ΚΚ Add the Runtime SmartHeap or Debug SmartHeap registry key as described in step 1 of γTo disable SmartHeap patching for all DLLs,δ above.

2.ΚΚΚ At the registry key that you added in step 1, add an entry with the following values:

αΚΚΚΚΚ Value name:Κ PatchTraceFile

αΚΚΚΚΚ Data type:Κ REG_SZ

αΚΚΚΚΚ Value:Κ The full path of a file name to which SmartHeap will log all patching activity.Κ SmartHeap will create the file if it does not exist.

First-chance exceptions reported by your debugger

When you run your application under both SmartHeap and your source debugger at the same time, your debugger may report one or more first-chance exceptions in the Debug SmartHeap DLL, ha312w32.dll.Κ These exceptions are caused by SmartHeapβs address validation, which uses Win32 structured-exception handling.Κ The exceptions are handled by SmartHeap and are not errors in your application or in SmartHeap.

SmartHeap for Win32 calling convention

In SmartHeap versions 2.0 and 2.1 for Win32, APIs were defined with the __cdecl calling convention.Κ SmartHeap 2.2 and higher uses __stdcall calling convention to define its APIs, which is the calling convention used for the Windows NT and Windows 95 system DLLs.


SmartHeap Win32φspecific values

The following are some platform-specific defaults and attributes for the Win32 version of SmartHeap (all values in bytes, non-debug version of SmartHeap; all values subject to change in future versions):

DescriptionΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ IntelΚΚΚ Alpha Related API, if any

Default page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚ 64KΚΚΚΚΚΚ MemPoolSetPageSize

Default γsmallδ block thresholdΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 256ΚΚΚΚΚΚΚ 256ΚΚΚΚΚΚΚ MemPoolSetSmallBlockSize

Per-alloc overhead, FS blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚ MemAllocFS

Per-alloc overhead, γsmallδ blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚ 0ΚΚΚΚΚΚΚΚΚΚΚ malloc, new, MemAllocPtr

Per-alloc overhead, var ptr blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 2ΚΚΚΚΚΚΚΚΚΚΚ 2ΚΚΚΚΚΚΚΚΚΚΚ malloc, new, MemAllocPtr

Per-alloc overhead, var handle blocksΚΚΚΚΚΚΚΚΚ 14ΚΚΚΚΚΚΚΚΚ 14ΚΚΚΚΚΚΚΚΚ MemAlloc

Granularity, all block typesΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 4ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ all

Minimum block size, FS blocksΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 4ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ MemAllocFS

Minimum block size, γsmallδ blocksΚΚΚΚΚΚΚΚΚΚΚΚ 4ΚΚΚΚΚΚΚΚΚΚΚ 8ΚΚΚΚΚΚΚΚΚΚΚ malloc, new, MemAllocPtr

Minimum block size, all othersΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 14ΚΚΚΚΚΚΚΚΚ 14ΚΚΚΚΚΚΚΚΚ malloc, new, MemAllocPtr,
ΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ MemAlloc

Per-page overhead, all APIsΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 40ΚΚΚΚΚΚΚΚΚ 40

Minimum page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚ 64KΚΚΚΚΚΚ MemPoolSetPageSize

Maximum page sizeΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚ 64KΚΚΚΚΚΚ MemPoolSetPageSize

Size of empty memory poolΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚΚ 64KΚΚΚΚΚΚ 64KΚΚΚΚΚΚ MemPoolInit[FS]


Default error handling in Win32

The default error handler in the Windows version of Runtime SmartHeap uses a task-modal message box (see MemDefaultErrorHandler in €4.2, γFunction referenceδ in the SmartHeap Programmerβs Guide).Κ If there is insufficient memory to display a task-modal message box, a system modal message box is displayed.

For non-recoverable errors, the default error handler shows an OK button and a Cancel button on the message box.Κ For recoverable errors, Abort, Retry, and Ignore buttons are shown.

Choosing either the Cancel or Abort button causes an int 3 (breakpoint interrupt) to be executed, which will stop in the debugger if a debugger is currently running.Κ If no debugger is running, SmartHeap will call the abort function, which terminates the current task.

Choosing either OK or Ignore causes the SmartHeap function that reported the error to return with an error return value.Κ Choosing Retry causes SmartHeap to retry the operation (for example, memory allocation).

In Debug SmartHeap, an Error Report dialog box is displayed if DBGMEM_OUTPUT_PROMPT is supplied to dbgMemSetDefaultErrorOutput (it is by default).Κ The Debug SmartHeap Error Report dialog box contains the following buttons:

αΚΚΚΚΚ Debug breaks to your source debugger, or if youβre not currently running under a debugger, this button opens the current Win32 γjust-in-timeδ debugger if you have installed one.

αΚΚΚΚΚ Continue resumes execution of your application.

αΚΚΚΚΚ Retry retries the failed operation -- this button is enabled only for out-of-memory errors.

αΚΚΚΚΚ Terminate closes your application.


αΚΚΚΚΚ The remaining buttons on the error report dialog box are available only if you have installed SmartHeapβs sister product, HeapAgent.Κ HeapAgent provides a graphical user interface for controlling heap debugging settings and diagnosing heap errors.Κ If you have HeapAgent, these buttons are used to open HeapAgent browsers, suppress errors, or get online help about a particular error.

If DBGMEM_OUTPUT_CONSOLE is supplied to dbgMemSetDefaultErrorOutput, output is sent to the OutputDebugString Win32 API, and will be displayed in the output window of the active debugger.Κ DBGMEM_OUTPUT_BEEP causes a MessageBeep on errors.