Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

759 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

15 Experts available now in Live!

Get 1:1 Help Now