View Issue Details

IDProjectCategoryView StatusLast Update
0035982FPCRTLpublic2019-08-23 17:00
ReporterAkira1364Assigned ToSven Barth 
Status resolvedResolutionfixed 
Platformx86_64OSWindowsOS Version10
Product Version3.3.1Product BuildTrunk 
Target VersionFixed in Version3.3.1 
Summary0035982: "TRTTIType.GetAttributes()" leaks memory when used in a for-in loop, unless you explicitly free the loop variable on every pass
DescriptionConsider the following example, adapted from Delphi's documentation on attributes, found here:

program BugExample;
{$mode Delphi}

uses RTTI;
  TSpecialAttribute = class(TCustomAttribute)
    FValue: String;
    constructor Create(const AValue: String);

  constructor TSpecialAttribute.Create(const AValue: String);
    FValue := AValue;

  [TSpecialAttribute('Hello World!')]
  TSomeType = record

  LContext: TRttiContext;
  LType: TRttiType;
  LAttr: TCustomAttribute;
  { Create a new Rtti context }
  LContext := TRttiContext.Create;
  { Extract type information for TSomeType type }
  LType := LContext.GetType(TypeInfo(TSomeType));

  { Search for the custom attribute and do some custom processing }
  for LAttr in LType.GetAttributes() do
    if LAttr is TSpecialAttribute then
  { Destroy the context }

When compiled with the -gh flag, the output after running is:

29 memory blocks allocated : 1545/1560
28 memory blocks freed : 1529/1544
1 unfreed memory blocks : 16
True heap size : 163840
True free heap : 163616
Should be : 163632
Call trace for block $00007FF307A0E2C
Steps To ReproduceCompile the provided program with -gh.
Additional InformationIn the event that this is actually how it's supposed to work, it should certainly then be documented as such.
Fixed in Revision42773
Attached Files


Sven Barth

2019-08-23 17:00

manager   ~0117801

Of course the attributes should be only released when the last context is released.

Please test and close if okay.

Issue History

Date Modified Username Field Change
2019-08-19 17:41 Akira1364 New Issue
2019-08-19 17:41 Akira1364 Tag Attached: attributes
2019-08-23 16:31 Sven Barth Assigned To => Sven Barth
2019-08-23 16:31 Sven Barth Status new => assigned
2019-08-23 17:00 Sven Barth Status assigned => resolved
2019-08-23 17:00 Sven Barth Resolution open => fixed
2019-08-23 17:00 Sven Barth Fixed in Version => 3.3.1
2019-08-23 17:00 Sven Barth Fixed in Revision => 42773
2019-08-23 17:00 Sven Barth FPCTarget => -
2019-08-23 17:00 Sven Barth Note Added: 0117801