11

I need to activate the ReportMemoryLeaksOnShutdown functionality to report the memory leaks of my application, but only under debug mode (when the Delphi IDE is running). How can I do this?

TrueWill
  • 25,132
  • 10
  • 101
  • 150
Salvador
  • 16,132
  • 33
  • 143
  • 245

3 Answers3

27

If you mean "debug mode" as compiled using the Debug build configuration (D2007+), you'll have the DEBUG symbol defined, so you can activate the ReportMemoryLeaksOnShutdown even when running oustide the debugger with:

{$IFDEF DEBUG}
  ReportMemoryLeaksOnShutdown := True;
{$ENDIF}

If you want to run only if the debugger is present, look at RRUZ answer

Community
  • 1
  • 1
jachguate
  • 16,976
  • 3
  • 57
  • 98
23

try using the DebugHook variable

ReportMemoryLeaksOnShutdown:=DebugHook<>0;
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 2
    And probably disable the stupid warning associated with `DebugHook`, `{$WARN SYMBOL_PLATFORM OFF}` – jasonpenny Mar 15 '11 at 20:20
  • 1
    Stupid (for the versions of Delphi I have,) because they only work on the Windows platform, so for me, it's just noise. – jasonpenny Mar 15 '11 at 20:28
  • 1
    I don't agree that the warning is stupid. I do realize that Kylix didn't get a follow-up and Delphi.NET got replaced by Delphi Prism. But with the things that future Delphi versions will bring (read the roadmap, demos during DelphiLive last year, and events this year some of which had x64 and/or Mac compiler previews), I think the warning can be annoying but is not stupid. – Jeroen Wiert Pluimers Mar 15 '11 at 20:29
  • @jason Well switch the warning off then and stop whining! Just because you don't need it doesn't mean many others want it. What's more the good folk at Emba gave you control of the warning. Good job in my view. – David Heffernan Mar 15 '11 at 20:30
  • @jasonpenny, only disable warnings as a last resort. Its always better to correct the problem first. – Toon Krijthe Mar 15 '11 at 20:33
  • Is there a global way to turn it off? I see it in the project options, but not global options. (I would change the word stupid in my comment, but there's no edit link showing for that one...) – jasonpenny Mar 15 '11 at 20:34
  • the project options are global. How much more global could it get? – David Heffernan Mar 15 '11 at 21:50
  • 1
    The project options are project specific (hence the name). There are still global options in the IDE valid for every project - they are just not the same options available. Although there is also something like default project options. – Uwe Raabe Mar 15 '11 at 22:19
4

I usually use the IsDebuggerPresent API function and also surround it with a DEBUG symbol check so the code doesn't end up in release builds:

{$IFDEF DEBUG}   
  ReportMemoryLeaksOnShutDown := IsDebuggerPresent();
{$ENDIF}

The function should already be declared in the Windows unit, if you're not using an ancient version of Delphi and works on Windows 2000 and newer.

jachguate
  • 16,976
  • 3
  • 57
  • 98
Goran Skledar
  • 221
  • 1
  • 6
  • IsDebuggerPresent is not declared in the Windows unit, so: `function IsDebuggerPresent: LongBool; stdcall;` `function IsDebuggerPresent; external kernel32 name 'IsDebuggerPresent';` – Reversed Engineer May 24 '19 at 12:49