Solved

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

Posted on 2008-10-29
5
377 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

792 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