The Memory Validator API
There are some features of Memory Validator that are useful to call directly from your program, including tracking of memory in custom heap managers.
Memory Validator has an API that makes this possible; just include stublib.h and link to these two libraries provided:
•svlMemoryValidatorStub.lib
The interface to the Memory Validator DLL
•svlMemoryValidatorStubLib.lib
An API library that calls the Memory Validator library
_x64.lib versions are available for the 64bit version of Memory Validator (when the executable is 64 bits).
Starting the Profiler
To start the profiler from your API code you need to call the function "startProfiler" from your code before you call any API functions. Ideally you should call this function as early in your program as possible.
Function Definition:
extern "C" void startProfiler();
If you prefer to start the profiler from the user interface or command line you can omit the startProfiler() call. You can leave it present if you wish to start Memory Validator from your program.
All the API functions are exported as extern "C", so C as well as C++ users can use them.
Linking with these libraries is only necessary in order to use the API from your program and is not needed if use Memory Validator 'normally'.
If you're using stublib.h (see below) a convenience function void mvStartProfiler() exists that calls startProfiler() for you.
Including stublib.h
The header stublib.h has all the appropriate function and enumeration definitions to use the API.
Ensure that you include <windows.h> prior to including stublib.h.
There are workarounds if you can't include <windows.h>.
Linking to the API libraries - 32 bit, x86
You'll need to link to both of the following files:
svlMemoryValidatorStub.lib is found in the following directories in the Memory Validator install directory:
•stub\debug
•stub\release
svlMemoryValidatorStubLib.lib is in the following directories where nnnn identifies the visual studio release, e.g. VS6, 2008, 2012, etc.
•stublib\debug_nnnn
•stublib\release_nnnn
Linking to the API - 64 bit, x64
Similar to 32 bit, but add _x64 to the library and the debug/release folder.
For example, svlMemoryValidatorStub_x64.lib is found in stub\debug_x64 and stub\release_x64.
Calling API functions using GetProcAddress
If you don't want to link to the API you can use GetProcAddress to find the interface functions in the Memory Validator DLL svlMemoryValidatorStub.dll.
The interface functions have different names and use C++ name mangling, but have identical parameters to the API functions.
To determine function names use a tool such as depends.exe
to examine the exports from Memory Validator.
Here is an example (with the 32 bit API):
HMODULE hMod;
// get module, will only succeed if Memory Validator launched this app or is injected into this app
hMod = GetModuleHandleA("svlMemoryValidatorStub.dll");
if (hMod != NULL)
{
// MV is present, lookup the function and call it to set a watermark for this location in the code
mvSetWatermark_FUNC pFunc;
// "?apiSetWatermark@@YAXPAD@Z" is equivalent to linking against "mvSetWatermark"
pFunc = (mvSetWatermark_FUNC)GetProcAddress(hMod, "?apiSetWatermark@@YAXPAD@Z"); // 32 bit example, not for x64!
if (pFunc != NULL)
{
(*pFunc)(watermarkName);
}
}
API functions and their GetProcAddress names
For any API functions not listed, try looking up the name in svlMemoryValidatorStub.dll using depends.exe
API Name
|
GetProcAddress() Name (32 bit, x86)
|
|
mvUserCustomAlloc
mvUserCustomReAlloc
mvUserCustomFree
mvUserCustomRefCountDecrement
mvUserCustomRefCountIncrement
mvUserDumpLeaks
mvSetHeapName
mvSetThreadName
mvSetWatermark
mvSetBookmark
mvIntegrityCheck
mvGarbageCollect
mvLeakDetect
mvLeakDetectFromWatermark
mvLeakDetectBetweenWatermarks
mvDetectUninitialised
mvSetCollect
mvGetCollect
mvEnableCRTHook
mvEnableGDIHook
mvEnableKernelHook
mvEnableSocketHook
mvEnableUserHook
mvEnableAllocHook
mvEnableTraceHook
mvEnableCoTaskHook
mvEnableShellHook
mvEnableComCtlHook
mvEnableIMallocHook
mvEnableMemCopyHook1
mvEnableMemCopyHook2
mvEnableMemCopyHook3
mvEnableMemCopyHook4
mvShutdownMemoryValidator
mvPushTracker
mvPopTracker
|
?informAlloc@@YAXPAXKKKK@Z
?informReAlloc@@YAXPAX0KKKK@Z
?informFree@@YAXPAXKKK@Z
?informRefCountDecrement@@YAXKKKK@Z
?informRefCountIncrement@@YAXKKKK@Z
?informDumpLeaks@@YA?AW4_dumpResult@@W4_dumpMethod@@PBDP6AHKK11HH@ZK@Z
?apiSetHeapName@@YAXPAXPAD@Z
?apiSetThreadName@@YAXKPAD@Z
?apiSetWatermark@@YAXPAD@Z
?apiSetBookmark@@YAXPAD@Z
?apiIntegrityCheck@@YAHXZ
?apiGarbageCollect@@YAHXZ
?apiLeakDetect@@YAHP6AXPAU_apiMemoryInfo@@PAX@Z1K@Z
?apiLeakDetectFromWatermark@@YAHPADP6AXKPAX@Z1@Z
?apiLeakDetectBetweenWatermarks@@YAHPAD0P6AXKPAX@Z1@Z
?apiDetectUninitialised@@YAHP6AXPAU_apiMemoryInfo@@PAX@Z1@Z
?apiSetCollect@@YAXH@Z
?apiGetCollect@@YAHXZ
?apiEnableCRTHook@@YAHW4_crtHookEnum@@H@Z
?apiEnableGDIHook@@YAHW4_hgdihEnum@@H@Z
?apiEnableKernelHook@@YAHW4_hkernelhEnum@@H@Z
?apiEnableSocketHook@@YAHW4_hsockethEnum@@H@Z
?apiEnableUserHook@@YAHW4_huserhEnum@@H@Z
?apiEnableAllocHook@@YAHW4_hallochEnum@@H@Z
?apiEnableTraceHook@@YAHW4_traceHookEnum@@H@Z
?apiEnableCoTaskHook@@YAHW4_hCoTaskEnum@@H@Z
?apiEnableShellHook@@YAHW4_hshellhEnum@@H@Z
?apiEnableComCtlHook@@YAHW4_hcomctlhEnum@@H@Z
?apiEnableIMallocHook@@YAHW4_himallochEnum@@H@Z
?apiEnableMemCopyHook1@@YAHW4_mchEnum1@@H@Z
?apiEnableMemCopyHook2@@YAHW4_mchEnum1@@H@Z
?apiEnableMemCopyHook3@@YAHW4_mchEnum1@@H@Z
?apiEnableMemCopyHook4@@YAHW4_mchEnum1@@H@Z
apiShutdownMemoryValidator
?apiPushTracker@@YAXPAD@Z
?apiPopTracker@@YAXXZ
|
|
API Name
|
GetProcAddress() Name (64 bit, x64)
|
|
mvUserCustomAlloc
mvUserCustomReAlloc
mvUserCustomFree
mvUserCustomRefCountDecrement
mvUserCustomRefCountIncrement
mvUserDumpLeaks
mvSetHeapName
mvSetThreadName
mvSetWatermark
mvSetBookmark
mvIntegrityCheck
mvGarbageCollect
mvLeakDetect
mvLeakDetectFromWatermark
mvLeakDetectBetweenWatermarks
mvDetectUninitialised
mvSetCollect
mvGetCollect
mvEnableCRTHook
mvEnableGDIHook
mvEnableKernelHook
mvEnableSocketHook
mvEnableUserHook
mvEnableAllocHook
mvEnableTraceHook
mvEnableCoTaskHook
mvEnableShellHook
mvEnableComCtlHook
mvEnableIMallocHook
mvEnableMemCopyHook1
mvEnableMemCopyHook2
mvEnableMemCopyHook3
mvEnableMemCopyHook4
mvShutdownMemoryValidator
mvPushTracker
mvPopTracker
|
?informAlloc@@YAXPEAX_K11K@Z
?informReAlloc@@YAXPEAX0_K11K@Z
?informFree@@YAXPEAX_K1K@Z
?informRefCountDecrement@@YAX_K00K@Z
?informRefCountIncrement@@YAX_K00K@Z
?informDumpLeaks@@YA?AW4_dumpResult@@W4_dumpMethod@@PEBDP6AH_K211HH@Z_K@Z
?apiSetHeapName@@YAXPEAXPEAD@Z
?apiSetThreadName@@YAXKPEAD@Z
?apiSetWatermark@@YAXPEAD@Z
?apiSetBookmark@@YAXPEAD@Z
?apiIntegrityCheck@@YAHXZ
?apiGarbageCollect@@YAHXZ
?apiLeakDetect@@YAHP6AXPEAU_apiMemoryInfo@@PEAX@Z1K@Z
?apiLeakDetectFromWatermark@@YAHPEADP6AXKPEAX@Z1@Z
?apiLeakDetectBetweenWatermarks@@YAHPEAD0P6AXKPEAX@Z1@Z
?apiDetectUninitialised@@YAHP6AXPEAU_apiMemoryInfo@@PEAX@Z1@Z
?apiSetCollect@@YAXH@Z
?apiGetCollect@@YAHXZ
?apiEnableCRTHook@@YAHW4_crtHookEnum@@H@Z
?apiEnableGDIHook@@YAHW4_hgdihEnum@@H@Z
?apiEnableKernelHook@@YAHW4_hkernelhEnum@@H@Z
?apiEnableSocketHook@@YAHW4_hsockethEnum@@H@Z
?apiEnableUserHook@@YAHW4_huserhEnum@@H@Z
?apiEnableAllocHook@@YAHW4_hallochEnum@@H@Z
?apiEnableTraceHook@@YAHW4_traceHookEnum@@H@Z
?apiEnableCoTaskHook@@YAHW4_hCoTaskEnum@@H@Z
?apiEnableShellHook@@YAHW4_hshellhEnum@@H@Z
?apiEnableComCtlHook@@YAHW4_hcomctlhEnum@@H@Z
?apiEnableIMallocHook@@YAHW4_himallochEnum@@H@Z
?apiEnableMemCopyHook1@@YAHW4_mchEnum1@@H@Z
?apiEnableMemCopyHook2@@YAHW4_mchEnum2@@H@Z
?apiEnableMemCopyHook3@@YAHW4_mchEnum3@@H@Z
?apiEnableMemCopyHook4@@YAHW4_mchEnum4@@H@Z
apiShutdownMemoryValidator
?apiPushTracker@@YAXPEAD@Z
?apiPopTracker@@YAXXZ
|
|
Other exported functions
You may see some other functions exported from svlMemoryValidatorStub.dll(_x64).dll.
These other functions are for Memory Validator's use. Using them may damage memory locations and/or crash your code. Best not to use them!