Solved

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

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

623 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