All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.delphi.rules.community-delphi.InlineDeclarationCapturedByAnonymousMethod.html Maven / Gradle / Ivy

There is a newer version: 1.12.1
Show newest version

Why is this an issue?

Up until Delphi 10.4.2, there are compiler bugs related to inline variables captured by anonymous methods:

  1. Inline variables will not have their lifetimes extended until the anonymous method goes out of scope. (Fixed in 10.4.1)
  2. Inline variables with managed types will not have their reference count updated when the anonymous method goes out of scope, causing memory leaks. (Fixed in 10.4.2)

How to fix it

Use a traditional variable declaration instead of an an inline declaration:

procedure Example;
begin
  var MyObj := TFoo.Create as IFoo;

  TTask.Run(
    procedure begin
      Sleep(1000);
      MyObj.Bar;
    end
  );
end;
procedure Example;
var
  MyObj: IFoo;
begin
  MyObj := TFoo.Create;

  TTask.Run(
    procedure begin
      Sleep(1000);
      MyObj.Bar;
    end
  );
end;

Resources





© 2015 - 2024 Weber Informatics LLC | Privacy Policy