


Setup
To allow MemPro to connect with your application you must compile your app with the MemProLib code and call the tracking functions when allocating memory. There are three main ways you can do this:
- Include MemPro.cpp in one of your source cpp files
- Compile and Link against the MemProLib.lib
- Link with MemProDll.dll
The MemProLib library can be found in the source code installation folder
that was set in the installation, the default is:
C:\Program Files\PureDevSoftware\MemPro
Note: if you are profiling an application with multiple dlls you must use option 3 and link MemProDll.dll.
1. Including MemPro.cpp
This is the recommended way of embedding the code. It is simple, and easy to comment out when not needed. It is also the most portable solution.
Add these three lines of code to your main cpp source file:
#define OVERRIDE_NEW_DELETE
#include "MemPro.cpp"
Instead of including the cpp file you can add the cpp file to your project and just include the .h file.
the cpp and h files can be found here:
C:\Program Files\PureDevSoftware\MemPro\MemProLib\src
The WAIT_FOR_CONNECT line is optional. If defined to true your application will block on startup until MemPro has connected. This allows you to be sure that you are tracking every single allocation.
And that's it!
2. Linking to the MemProLib library
If you prefer to link to the MemPro library, include the MemPro.h in your main.cpp
#define OVERRIDE_NEW_DELETE
#include "MemPro.h"
And link to one of the pre-built libraries in the lib folder.
The header file can be found here:
C:\Program Files\PureDevSoftware\MemPro\MemProLib\src
The lib files can be found here
C:\Program Files\PureDevSoftware\MemPro\MemProLib\lib
If none of the pre-built libraries are suitable you can build your own MemProLib from
the source code in the src file. MemProLib builds on virtually any platform and OS. The
MemProLib source code can be found here:
C:\Program Files\PureDevSoftware\MemPro\MemProLib\src
3. Linking to the MemProLib Dll
Add these two lines to a cpp source file in each dll that you want to profile
include "MemProDll.h"
Copy the MemProDLL.dll file to your exe directory.
The header file can be found here:
C:\Program Files\PureDevSoftware\MemPro\MemProDll\include
The dll and lib files are here:
C:\Program Files\PureDevSoftware\MemPro\MemProDll\bin
MemProDll.h includes a pragma telling the linker to link to MemProDll.lib. This adds a dependency to the dll. Please make sure the linker can find the lib file by adding the necessary path or modifying the pragma.
Note: Make sure that MemProDll.h is included only _once_ in each dll. Put it in the main source file, do not put it into a header that is included in multiple places.
Include and Library Paths
The installer automatically adds the include and library paths to the INCLUDE and LIB environment variables. However, the IDE does not use these variables unless run with the /useenv command line switch. So you'll probably want to add the path in the global property sheet in the IDE.
If you have already overridden new and delete
In the above solutions the OVERRIDE_NEW_DELETE define tells MemPro to override new and delete with its tracking hooks inserted. If you have already overridden new and delete (maybe for your own memory manager) you will need to insert the MemPro hooks into your overridden functions:
Include MemPro.cpp as before but don't define OVERRIDE_NEW_DELETE. Instead use the MemPro::TrackAlloc and MemPro::TrackFree functions
bool g_WaitForConnect = false;
void* operator new(size_t size)
{
void* p = YourInternalAlloc(size);
MemPro::TrackAlloc(p, size, g_WaitForConnect);
return p;
}
void operator delete(void* p)
{
MemPro::TrackFree(p, g_WaitForConnect);
YourInternalFree(p);
}
void* operator new[](size_t size)
{
void* p = YourInternalAlloc(size);
MemPro::TrackAlloc(p, size, g_WaitForConnect);
return p;
}
void operator delete[](void* p)
{
MemPro::TrackFree(p, g_WaitForConnect);
YourInternalFree(p);
}
The g_WaitForConnect bool specifies whether the call blocks until the MemPro app has connected. This is useful for making sure absolutely every allocation is being tracked.
What if I can't use a winsock connection in my app?
MemProLib uses a winsock TCP connection to communicate with MemPro. If for some reason this isn't possible in your application (no network connection for example) then you can still use MemPro.
Instead of connecting to your application over TCP you can tell MemProLib to write out a dump file of all allocations. This dump file can then be opened in MemPro and analyised just like any other mempro file. To enable allocation dumping uncomment this line in MemPro.h
When this define is enabled the specified file will be created and all operations will be written to this file instead of being set over the network. The define also removes any dependency on windows sockets, so you should be able to use it in any environment. This file can get pretty big, so please ensure you have enough disk space.
Note: To avoid linker errors when overriding new/delete, make sure that MemPro.cpp is included before any system headers that also override new/delete. You may need to turn off any pre-compiled headers for that file. You may also need to do a full re-compile of your app to avoid multiple defined new and delete function warnings.
If you have any problems setting up MemProLib:
- see the FAQ on the website http://www.puredevsoftware.com/mempro_faq.htm
- email [email protected]