473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Memory leak and buffer overrun detection code in VC7.1

Environment: WinXP, VC++ 7.1 Standard Edition
~~~~~~~~~~~

I have a set of functions that I invoke from main() function of a sample
console app.
These are the functions:
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
HANDLE hLogFile = CreateFile(_T("D:\\Temp\\dump.txt"),
GENERIC_WRITE|GENERIC_READ,0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL);

_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);

_CrtSetReportFile(_CRT_WARN, hLogFile);

_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);

_CrtSetReportFile(_CRT_ERROR, hLogFile);
These are supposed to catch memory leaks and array bounds overflow etc and
then dump the data into the designated text file.

My console app is built with /RTC1 ( = /RTCsu ) flag to detect stack /
buffer overflows.

Now if I introduce a buffer overflow (accessing over last element of a C
style array), the error is properly redirected to the text file and I can
see the results in the txt file.

Problem:
---------
Obviously, I have many such exes where I wish to introduce this run time
debug checks. So the idea was to have a common dll which everyone uses and
then put these calls from within the DllMain of the dll. So if any exe just
links against this dll, it automatically has called these functions through
the DllMain.

I coded these functions in the PROCESS_ATTACH part of the common dll. When
the exe runs, I can see that the DllMain and these functions are getting
called. However, now when the program terminates, there is no dump
generated.

I can see that one thing: if the leak is in main() module, the check
functions have to be called from there.
If the leak / overflow is there in the Dll, the calls have to be made from
within the Dll.

Is there any way in which I can make the call within the Dll and not expect
every exe to also change?

--
~~~~~
For replying directly to me, please remove the 'nospam' from my email id as
it appears
Nov 17 '05 #1
5 1859
Senapathy wrote:
Is there any way in which I can make the call within the Dll and not expect
every exe to also change?


You need to link against the DLL-Version of the CRT! (both the EXE and
the DLL).

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2

"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
Senapathy wrote:
Is there any way in which I can make the call within the Dll and not
expect every exe to also change?
You need to link against the DLL-Version of the CRT! (both the EXE and the
DLL).


Sorry, I didnt quite understand what you meant. Do you mean that I should
explicitly link against MSVCR71.dll explicitly? (in the linker settings)
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #3
Senapathy wrote:
Is there any way in which I can make the call within the Dll and not
expect every exe to also change?


You need to link against the DLL-Version of the CRT! (both the EXE and the
DLL).


Sorry, I didnt quite understand what you meant. Do you mean that I should
explicitly link against MSVCR71.dll explicitly? (in the linker settings)


I mean that you should go into your project settings (C/C++ | Code
generation) and change the entry
"Runtime library" to "Multi-Threaded (Debug) DLL (/MD(d))"

If you are using the MFC you also need to change the same stuff for the
MFC-settings (general-page: "Use MFC in a shared DLL").

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #4
I am beyond words to express my thanks !
Your solution is perfect ! So simple and yet the exact answer that I was
looking for.

Thank you so much for sharing the knowledge with me!!!

Btw, just out of curiosity - from where did you get this information? From
sheer experience or from some documentation?
Reason I am asking is: in the MSDN documentation for the _CrtSetxx
functions, it is not documented that it should be done this way...

Regards,
Sena

"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Senapathy wrote:
Is there any way in which I can make the call within the Dll and not
expect every exe to also change?

You need to link against the DLL-Version of the CRT! (both the EXE and
the DLL).


Sorry, I didnt quite understand what you meant. Do you mean that I should
explicitly link against MSVCR71.dll explicitly? (in the linker settings)


I mean that you should go into your project settings (C/C++ | Code
generation) and change the entry
"Runtime library" to "Multi-Threaded (Debug) DLL (/MD(d))"

If you are using the MFC you also need to change the same stuff for the
MFC-settings (general-page: "Use MFC in a shared DLL").

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Nov 17 '05 #5
Senapathy wrote:
I am beyond words to express my thanks !
Your solution is perfect ! So simple and yet the exact answer that I
was looking for.

Thank you so much for sharing the knowledge with me!!!

Btw, just out of curiosity - from where did you get this information?
From sheer experience or from some documentation?
Reason I am asking is: in the MSDN documentation for the _CrtSetxx
functions, it is not documented that it should be done this way...


If you don't link everything against the DLL version of the runtime, then
you have multiple versions of the runtime in the resulting process. Setting
the error mode (etc) for the runtime in the DLL has no effect on the other
runtime image(s) that are loaded in the process.

As a general rule, if you need to share runtime state across modules, you
need to link everything with the DLL runtime library. Runtime state
includes global settings (like you experienced), FILE* values, memory
allocations, C "file handles" (not to be confused with operating system
HANDLEs) and probably a few other things that I'm missing at the moment :)
Note that any sharing of C++ standard library components involves sharing of
memory allocations, so if you want to pass a std::vector from an EXE to a
DLL, they should both be linked to the DLL runtime library.

-cd
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Qin Zhou | last post by:
This is possible memory leak can be reproduced by the following code. I am hoping someone can help me out with simple solutions! Thanks in advanced! using System; using System.IO; using...
8
by: __jakal__ | last post by:
Hello, Is there any good memory leak detection software for C++ available as a freeware... I had used purify but had to discontinue due to huge license fees... Also tried Sun workshop memory...
7
by: mosaic | last post by:
Hi, all I really interested in how to check the memory leak of a program. Your smart guys, do you have excellent ideas that could share with me? Thank you. The following is my idea: In C...
0
by: Frank Lopez | last post by:
Does anyone know if Microsoft generated a whitepaper on this topic? Does anyone know what the solution is? (meaning, eliminate the leak problem -- I am seeing three memory leaks from...
5
by: prashantkhoje | last post by:
hi ppl, i am developing an C application in Microsoft visual C++ 6.0. i get following error while running it in debug mode: /* here's the error message */ program: my_application.exe...
8
by: kk_oop | last post by:
Hi. Any recommendations for memory leak detection tools for C++ running on Linux or Solaris? I'm interested in static (compile time) and runtime detection. It's for a large project. Thanks! ...
14
by: Ook | last post by:
I have a set<Item*that I use in a class, Item being one of my classes. In the destructor, I want to iterate through the set and remove the elements so there are no memory leaks. Is this the correct...
4
by: O.B. | last post by:
I've got this C# .NET 2005 application that has some unmanaged code. At random times, I get: An unhandled expection of type 'System.AccessViolationException' occurred in Unknown Module. ...
16
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.