Oracle Consulting Oracle Training Oracle Support Development
Home
Catalog
Oracle Books
SQL Server Books
IT Books
Job Interview Books
eBooks
Rampant Horse Books
911 Series
Pedagogue Books

Oracle Software
image
Write for Rampant
Publish with Rampant
Rampant News
Rampant Authors
Rampant Staff
 Phone
 800-766-1884
Oracle News
Oracle Forum
Oracle Tips
Articles by our Authors
Press Releases
SQL Server Books
image
image

Oracle 11g Books

Oracle tuning

Oracle training

Oracle support

Remote Oracle

STATSPACK Viewer

Privacy Policy

 

   
  SQL Server Tips by Burleson

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; \
_CrtMemCheckpoint(&memState);

#define XP_MEM_DIFF(memState) \
CExtendedStoredProcedure::CheckForHeapLeaks(&memState);

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

#endif

#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
XP_ASSERT(_CrtCheckMemory());

//Get the current memory state
_CrtMemState endMemState;
_CrtMemCheckpoint(&endMemState);

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

XP_ASSERT(FALSE);
}
}
#endif


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

 http://www.rampant-books.com/book_2005_2_sql_server_external_procedures.htm  

Linux Oracle commands syntax poster

ION Oracle tuning software

Oracle data dictionary reference poster



Oracle Forum

BC Oracle consulting support training

BC remote Oracle DBA   

 

   

 Copyright © 1996 -2016 by Burleson. All rights reserved.


Oracle® is the registered trademark of Oracle Corporation. SQL Server® is the registered trademark of Microsoft Corporation. 
Many of the designations used by computer vendors to distinguish their products are claimed as Trademarks
 

Hit Counter