Solved

How to detect memory leaks in a DLL file without having main program

Posted on 2008-10-29
5
372 Views
Last Modified: 2013-12-14
Background:
I have the code for a .DLL file which is a plug-in for a web proxy server.  The purpose of this .DLL is to provide authentication for the account which returns a result to the web proxy indicating if the user should be allowed to pass through.

Problem:
The functionality works fine, however, when we gave it a load test after many hours the proxy server crashes.  I believe this is due to a possible resource issue within this .DLL file since when we disable using it, the proxy appears to be stable through the load test.

I do not have the code to the proxy server to embed any type of memory leak detection; I only have the code for the DLL (written in C).  I am able to compile this code using Visual C++ 2008 Express.

Is there a utility or approach I can take to detect if there are any issues within this DLL without having source code to the main program that calls this DLL?

I have done a quick search here, however, I did not see anything that was specific to ONLY having the DLL code and not the main program.

Thanks
0
Comment
Question by:kylewatson44
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 22833299
You don't need the code of the executable to detect memory leaks in your DLL. Add the following to a central header file:

#ifdef _DEBUG
#ifndef _DBG_NEW

#include <crtdbg.h>

inline void* __operator_new(size_t __n) {
     return ::operator new(__n,_NORMAL_BLOCK,__FILE__,__LINE__);
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
     return ::operator new(__n,_NORMAL_BLOCK,__fname,__line);
}
inline void _cdecl operator delete(void* __p,const char*,int) {
     ::operator delete(__p);
}

#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW


#endif // _DBG_NEW
#else

#define __operator_new(__n) operator new(__n)

#endif

Then, add

               int tmpFlag;

               // Get the current state of the flag
               // and store it in a temporary variable
               tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

               // Turn On (OR) - Keep freed memory blocks in the
               // heap’s linked list and mark them as freed
               tmpFlag |= _CRTDBG_LEAK_CHECK_DF;

               // Set the new state for the flag
               _CrtSetDbgFlag( tmpFlag );


to your 'DllMain()' start code and call

               _CrtDumpMemoryLeaks ();


to before the DLL is unloaded and you'll get the line number where the allocation occured.

(Ref.: http:Q_21009673.html)
0
 

Author Comment

by:kylewatson44
ID: 22834367
This looks like it will do just what I need for new/delete.  This is written in C and uses malloc/free.  I will search for a similar solution and also check back here in case I am mistaken.  I will update again after I spend some time on this.  Thank you for the response!
0
 
LVL 86

Expert Comment

by:jkr
ID: 22834423
The is same done for 'malloc()' and 'free()' automatically in the debug version of the CRT. The above only adds the identical support for C++-style dynamic object allocation.
0
 
LVL 86

Expert Comment

by:jkr
ID: 23195739
Since a full solution was posted, I'd like to object to a deletion.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now