Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / Visual Basic

Easy Detection of Memory Leaks

Rate me:
Please Sign up or sign in to vote.
4.73/5 (25 votes)
4 Aug 20058 min read 186.6K   8.6K   122  
Describes a tool for easy detection of memory leaks.
#ifndef __MemoryHooksAPI_h__included__B17083B6_6EA8_4c5c_AA2C_8E7B5078A83E
#define __MemoryHooksAPI_h__included__B17083B6_6EA8_4c5c_AA2C_8E7B5078A83E

//////////////////////////////////////////////////////////
// MEMORY HOOKS API
//
// migo (2005)
//
// Comments / suggestions / bug reports are welcome:
// mgopshtein@gmail.com
//////////////////////////////////////////////////////////

#ifdef MEMORYHOOKS_EXPORTS
#define MEMORYHOOKS_API extern "C" __declspec(dllexport)
#else
#define MEMORYHOOKS_API extern "C" __declspec(dllimport)
#endif


////////////////////////////////////////////
// 
// Initialization
//
////////////////////////////////////////////
MEMORYHOOKS_API BOOL InitMemoryHooks();
MEMORYHOOKS_API BOOL UninstallMemoryHooks();


////////////////////////////////////////////
// 
// Use StartMemoryMonitoring to start monitoring - all
// memory allocations and releases will be registered
// 
// After a call to EndAllocationsMonitoring, further allocations will
// be ignored, but releases will be still watched.
//
// With EndMemoryMonitoring all monitoring will stop, and this
// current state can be used for further analysis.
///////////////////////////////////////////
MEMORYHOOKS_API BOOL StartMemoryMonitoring();
MEMORYHOOKS_API BOOL EndAllocationsMonitoring();
MEMORYHOOKS_API BOOL EndMemoryMonitoring();


////////////////////////////////////////////
// 
// WriteLeakImage is used for analysis and reports. It should
// be used when all memory monitoring is already stopped. 
///////////////////////////////////////////
MEMORYHOOKS_API BOOL WriteLeakImage( const char *sFileName, int bDumpBuffers, int iMaxNumOfStacks );


////////////////////////////////////////////
// 
// Adding automatic Breakpoints
//
// When in monitoring mode, each memory allocation is registered,
// along with it's stack information, including the occurrence number
// of the same stack from the time the monitoring started.
// The breakpoint will be activated on same occurrence of the stack
// (starting from a call to 'ActivateBreakpoints'), as the occurrence
// that created the memory leak.
//
// For example, assume that 5 allocations were made from stack S, but
// only 1st, 3rd and 4th were released. We can say that there are memory
// leaks in S(2) and S(5). For this example, after we call 'ActivateBreakpoints'
// function, execution will stop at the 2nd time an allocation is made
// from stack S.
//
// For programs with "iterative" patters we assume that it will behave the same
// on each iteration, and so same memory leaks will occur.
//
// This technique can be combined with Save and Load function (described below).
// First time the application runs, the memory leaks can be traced and saved
// in file. Next time we can load this info in the place, where we started
// monitoring, and then activate breakpoints
///////////////////////////////////////////
MEMORYHOOKS_API BOOL ActivateBreakpoints( int iNumOfBreakPoints );


////////////////////////////////////////////
// 
// Remote invocation
//
// Following functions are "wrappers" to previously defined API,
// and enable operating memory hooks on other process.
//
// NOTE: In current version all "remote" functions are "void".
// Smarter mechanism for returning a status between the processes
// may be added later
///////////////////////////////////////////
MEMORYHOOKS_API BOOL RemoteAttachToProcess( DWORD pid );
MEMORYHOOKS_API void DetachFromRemoteProcess();

MEMORYHOOKS_API void RemoteStartMemoryMonitoring();
MEMORYHOOKS_API void RemoteStopAllocationMonitoring();
MEMORYHOOKS_API void RemoteStopAllMemoryMonitoring();

MEMORYHOOKS_API void RemoteWriteLeaksToLog( LPCTSTR strFileName, BOOL bIncludeData, int iMaxNumOfStacks );
MEMORYHOOKS_API void RemoteActivateBreakpoints( int iMaxNumOfStacks );


// This function is used internally by remote invocation process
MEMORYHOOKS_API DWORD InitializeInTargetProcess();


#endif  // include once

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions