Link to home
Start Free TrialLog in
Avatar of shawn857
shawn857

asked on

How to invoke "ReportMemoryLeaksOnShutdown"

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
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shawn857
shawn857

ASKER

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
Yes. This setting is located in FastMM4Options.inc. Make it:
{$define EnableMemoryLeakReporting}

Open in new window

This include file is very well documented.
Thanks Sinisav. I've heard it's quite tricky to install. Is there instructions on how to install it (for Delphi 7) ?

Thanks
   Shawn
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
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.
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.
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
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.
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.
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
Thanks guys, I split the points up for you.

Cheers!
   Shawn