Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-29
5
Medium Priority
?
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

722 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