Solved

How to invoke "ReportMemoryLeaksOnShutdown"

Posted on 2013-11-30
14
2,167 Views
Last Modified: 2013-12-09
Hi, I think I'm getting some pretty serious memory leaks in my Delphi 7 app - just a little while ago I got this:

---------------------------
Debugger Fault Notification
---------------------------
Project C:\Myapp.exe faulted with message: 'privileged instruction at 0x005938be'. Process Stopped. Use Step or Run to continue.
---------------------------
OK  
---------------------------

... so looking around on the web I saw that there's a global variable "ReportMemoryLeaksOnShutdown" that just needs to be set to True that will report the memory leaks. I tried to employ this in my code, but it gives a compile error "Undeclared Identifier: ReportMemoryLeaksOnShutdown". Is there some special unit this command is hiding in? I thought it was globally available?

Thanks!
    Shawn
0
Comment
Question by:shawn857
  • 6
  • 5
  • 3
14 Comments
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 300 total points
ID: 39688700
Delphi 7 doesn't have support for detecting memory leak "by default". This comes from using FastMM. Include unit FastMM4 in dpr source as first unit...
0
 

Author Comment

by:shawn857
ID: 39688710
oh okay, thanks Sinisav. So I just go to the link you gave and download and install this FastMM4? Then  the "ReportMemoryLeaksOnShutdown" will work?

Thanks
   Shawn
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39688759
Yes. This setting is located in FastMM4Options.inc. Make it:
{$define EnableMemoryLeakReporting}

Open in new window

This include file is very well documented.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:shawn857
ID: 39688865
Thanks Sinisav. I've heard it's quite tricky to install. Is there instructions on how to install it (for Delphi 7) ?

Thanks
   Shawn
0
 

Author Comment

by:shawn857
ID: 39689162
OK Sinisav, I downloaded and unzipped FastMM and included the FastMM4 unit in the uses clause of my dpr file. I'm unfamiliar with using Include files in Delphi so I didn't exactly understand what you meant by your comment:

"Yes. This setting is located in FastMM4Options.inc. Make it:
{$define EnableMemoryLeakReporting}"

I looked in the FastMMOptions.inc file and see where it says:

"{Set this option to enable reporting of memory leaks. Combine it with the two
 options below for further fine-tuning.}
{$define EnableMemoryLeakReporting}"

... but how do I "set this option"? I don't know how to do this. Like I said, I'm not familiar with how to use include files - this is uncharted territory for me. Anyway, I compiled and built my app with FastMM in it and ran it and then closed my app - it didn't give me a memory leak report like I thought it would   :-(
   Sorry for all the stupid/naive questions - this FastMM/IncludeFile/finding memory leak stuff is all new to me. ...

Thanks
   Shawn
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 39689256
Hi Shawn,

FastMM is a great tool but with a bit of a steep learning curve. Alternatively you can install the trial of Eurekalog which also has a memory leak detection. It's not as sophisticated as FastMM, but it might help you to start.
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39689265
Thic include file is compiler directives. If something is marked as:
{$define something} then in code where you use code {$ifdef something}..{$endif} whole block of commands will be available. Somtimes for disabling this block of source you can "undef "/ "disable" these dirctives with putting point like here: {.$define something}
If you see these option like: {$define EnableMemoryLeakReporting} all shoud be fine.
Maybe you can combine other options (remove point if exists) for extra settings. Do full build of project and after app is done report should be shown.
0
 

Author Comment

by:shawn857
ID: 39690597
Thanks guys... after some googling on FastMM, I found this little utility which is very helpful in setting up the options of FastMM in a user friendly way;

http://delphibistro.com/?p=186

Initially, I want to just run my app (outside of the IDE debugger), then close it, and be shown a list of the memory leaks (like the way the "ReportMemoryLeaksOnShutdown" global variable works in D2007). I set my options accordingly in this utility (I hope correctly), and ran my app... and closed it - it indeed displayed a window showing the leak information:


---------------------------
MyApp.exe: Memory Leak Detected
---------------------------
This application has leaked memory. The small block leaks are (excluding expected leaks registered by pointer):


5 - 12 bytes: TCSVBasicRecord x 59, AnsiString x 136

13 - 20 bytes: AnsiString x 2

29 - 36 bytes: AnsiString x 4, Unknown x 2

X 37 - 44 bytes: TSystemInfo x 1, AnsiString x 12

45 - 52 bytes: TStringList x 120

53 - 60 bytes: AnsiString x 839

101 - 108 bytes: Unknown x 118

1981 - 2172 bytes: Unknown x 22

2173 - 2380 bytes: TGpTextFile x 22


Note: To obtain a log file containing detail on memory leaks, enable the "FullDebugMode" and "LogMemoryLeakDetailToFile" conditional defines. To disable this memory leak check, undefine "EnableMemoryLeakReporting".


... quite a lot of leaks. I'm assuming the "x 22" or "x 120" at the end of each leak description refers to the number of times each leak occurred, yes? This report file is helpful, but what I really need is to know which of my variables/declarations are the culprits. Will the "FullDebugMode" tell me exactly this? If so, how exactly do I run this FullDebugMode?

MerjinB - thanks for telling me about EurekaLog. This looks like something I would really want to have in my app when I do deploy it.

Thanks!
    Shawn
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 39691802
Shawn, did you get stack traces in your log file?

Oh, and Eurekalog is really a worthwhile investment. There is an alternative: http://madshi.net/index.htm, but I don't have any experience with that.
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39692074
I don't know how your code looks like, but it is always good practice do deallocate objects/memory after usage.

...
lst := TStringList.Create;
try
  lst.....
finally
  lst.Free;
end;

Open in new window


  fil := TGpTextFile.Create('c:\test.txt');
  try
    fil.Reset; 
    fil....
  finally 
    fil.Free; 
  end;

Open in new window



....or if you create something in OnCreate event of Form then do free up in OnDestroy event.

When you run app in Delphi - did you get this error? Error should be easy located in debug mode.
0
 

Author Comment

by:shawn857
ID: 39693711
Thanks guys, I have been hard at work plugging the memory leaks and have been successful finding them... but still more to go. That little utility I found for configuring FastMM really helped. I guess my only questions are, how does the "FullDebugMode" work in FastMM? Does it show the memory leaks "as the happen" while you in debug mode? (I can't visualize that, seeing as how you need to close out your app in order to see which memory is not being freed properly).
   Also, when I am done fixing leaks and want to distribute my app to my users, how do I configure FastMM to simply utilize it's memory management features?

Thanks!
   Shawn
0
 
LVL 19

Assisted Solution

by:MerijnB
MerijnB earned 200 total points
ID: 39694988
You can't see leaks 'while they happen', this is because FastMM doesn't know when you should free the memory (maybe you free the memory when the application closes), so FastMM can only call something a 'leak' when at the closure of your application it still isn't freed.

If you add the FastMM_FullDebugMode.dll to the folder where your executable is, you will also see stack traces where the memory allocation for each leak occured.

You can use the define $RELEASE found in FastMM4Options.inc when you build a release version of your app.
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39695077
Purpose of FastMM is faster memory allocation and usage in application (faster strings manipulation). It replaces Borlands memory allocation unit which is slower. Side effect is that FastMM knows what is allocated (because FastMM is do it) and what is deallocated.
FastMM doesn't "know" your code. It shows what is used and waht is not released upon application destroying. You will never "clean" your app for all "leaks" but make sure to clean largest...
0
 

Author Closing Comment

by:shawn857
ID: 39707192
Thanks guys, I split the points up for you.

Cheers!
   Shawn
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
indy Tmemory stream non latin charcter issue 2 121
QRReport  TQrmemo vertical stretching 1 53
Delphi inherited method 6 76
Wincontrol not (correctly) drawn 15 42
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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