Re: Three memory leaks found

Giganews Newsgroups
Subject: Re: Three memory leaks found
Posted by:  Remy Lebeau (re…@lebeausoftware.org)
Date: Fri, 29 Apr 2016

Graeme wrote:

> when I quite the application, I get 3 reported memory leaks.

Those are intentional leaks:

IdStack.pas:

{code}
finalization
  // Dont Free. If shutdown is from another Init section, it can cause GPF
when stack
  // tries to access it. App will kill it off anyways, so just let it leak
  {$IFDEF FREE_ON_FINAL}
  FreeAndNil(GStackCriticalSection);
  {$ENDIF}
{code}

IdThread.pas:

{code}
finalization
  ...
  {$IFDEF FREE_ON_FINAL}
  //only enable this if you know your code exits thread-clean
  FreeAndNil(GThreadCount);
  {$ENDIF}
{code}

FREE_ON_FINAL is not defined by default (see IdCompilerDefines.inc):

{code}
// $DEFINE the following if the global objects in the IdStack and IdThread
// units should be freed on finalization
{.$DEFINE FREE_ON_FINAL}
{$UNDEF FREE_ON_FINAL}
{code}

On systems that support leak reporting (including FastMM, MadExcept, etc),
Indy registers these leaks so they are not reported in leak reports:

IdStack.pas:

{code}
initialization
  ...
  GStackCriticalSection := TIdCriticalSection.Create;
  {$IFNDEF DOTNET}
    {$IFDEF REGISTER_EXPECTED_MEMORY_LEAK}
  IndyRegisterExpectedMemoryLeak(GStackCriticalSection);
    {$ENDIF}
  {$ENDIF}
{code}

IdThread.pas:

{code}
initialization
  ...
  GThreadCount := TIdThreadSafeInteger.Create;
  {$IFNDEF FREE_ON_FINAL}
    {$IFDEF REGISTER_EXPECTED_MEMORY_LEAK}
  IndyRegisterExpectedMemoryLeak(GThreadCount);
  IndyRegisterExpectedMemoryLeak(TIdThreadSafeIntegerAccess(GThreadCount).FCriticalSection);
    {$ENDIF}
  {$ENDIF}
{code}

> I'm using FPC 2.6.4 with Indy 10.6.2 (from repository) and my
> application is compiled with the -gl compiler parameter which
> enables the leak detection.

I was not aware of that feature.  Looking at it now, it does not appear to
provide an option to hide expected leaks.

--
Remy Lebeau (Indy Team)

Replies

None

In response to

Three memory leaks found posted by Graeme Geldenhuys on Thu, 28 Apr 2016