Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2947
  • Last Modified:

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
0
shawn857
Asked:
shawn857
  • 6
  • 5
  • 3
2 Solutions
 
Sinisa VukCommented:
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
 
shawn857Author Commented:
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
 
Sinisa VukCommented:
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: 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.

 
shawn857Author Commented:
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
 
shawn857Author Commented:
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
 
MerijnBSr. Software EngineerCommented:
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
 
Sinisa VukCommented:
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
 
shawn857Author Commented:
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
 
MerijnBSr. Software EngineerCommented:
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
 
Sinisa VukCommented:
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
 
shawn857Author Commented:
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
 
MerijnBSr. Software EngineerCommented:
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
 
Sinisa VukCommented:
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
 
shawn857Author Commented:
Thanks guys, I split the points up for you.

Cheers!
   Shawn
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now