Call now: (800) 766-1884  


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA

 SQL Server Scripts
 Scripts Menu




  SQL Server Tips by Gama and Naughter

Track Memory Leaks

Recent versions of Microsoft Visual C++ include a debug version of the “C” runtime which tracks heap allocations. When you build a debug version of your dll, the framework automatically uses these functions in the generated exported function. The framework automatically takes a snapshot of the heap using the function ‘_CrtMemCheckpoint’ at the start of the function, and just before the function returns. If it sees any differences, which would indicate a memory leak in your XP’s code, the framework calls ‘_CrtDumpMemoryLeaks’ which prints out all the memory allocations to the debug output window in your debugger. The framework code was updated as follows to support this functionality:

#ifdef _DEBUG
#define XP_MEM_STATE(memState) \
_CrtMemState memState; \

#define XP_MEM_DIFF(memState) \

#define XP_MEM_STATE(memState)
#define XP_MEM_DIFF(memState)


#define IMPLEMENT_XP(xpName, class) \
extern "C" SRVRETCODE __declspec(dllexport) xpName(SRV_PROC* srvproc) \
{ \
XP_MEM_STATE(beginMemState) \
SRVRETCODE retCode = 0; \
{ \
class _xp; \
retCode = _xp.main(srvproc, #xpName); \
} \
XP_MEM_DIFF(beginMemState) \
return retCode; \

#ifdef _DEBUG
void CExtendedStoredProcedure::CheckForHeapLeaks(_CrtMemState* pBeginMemState)
//Verify the integrity of the heap as it stands currently

//Get the current memory state
_CrtMemState endMemState;

//Check to see if there is a difference
_CrtMemState diffMemState;
int nMemDiff = _CrtMemDifference(&diffMemState, pBeginMemState, &endMemState);
if (nMemDiff)


Notice how in the exported function, ‘{‘ scoping is using to ensure the _xp instance gets destructed before the memory check is done. This technique is quite general and is a way of ensuring local variables are destroyed in a function before they normally would. In addition the XP_ASSERT macro is provided by the framework, which you can use in your XP code to provide ASSERT functionality. The XP_ASSERT macro is implemented to use the correct assert depending on the inclusion of the MFC or ATL framework in your XP project.

By using the allocation numbers reported by ‘_CrtDumpMemoryLeaks’ in combination with another CRT function called ‘_CrtSetBreakAlloc’, you can very quickly track down and fix any memory leaks in your code. As shown above, the framework also calls ‘_CrtCheckMemory’ to verify the integrity of the heap. For further information on using the features of the Debug CRT, please refer to the MSDN documentation.

The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter  


Burleson Consulting Remote DB Administration







Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.

Burleson Consulting
SQL Server database support


Copyright © 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter