"TRTTIType.GetAttributes()" leaks memory when used in a for-in loop, unless you explicitly free the loop variable on every pass
Original Reporter info from Mantis: Akira1364
-
Reporter name:
Original Reporter info from Mantis: Akira1364
- Reporter name:
Description:
Consider the following example, adapted from Delphi's documentation on attributes, found here:
http://docwiki.embarcadero.com/RADStudio/Rio/en/Extracting_Attributes_at_Run_Time
program BugExample;
{$mode Delphi}
uses RTTI;
type
TSpecialAttribute = class(TCustomAttribute)
public
FValue: String;
constructor Create(const AValue: String);
end;
constructor TSpecialAttribute.Create(const AValue: String);
begin
FValue := AValue;
end;
type
[TSpecialAttribute('Hello World!')]
TSomeType = record
end;
var
LContext: TRttiContext;
LType: TRttiType;
LAttr: TCustomAttribute;
begin
{ 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
Writeln(TSpecialAttribute(LAttr).FValue);
{ Destroy the context }
LContext.Free;
end.
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 reproduce:
Compile the provided program with -gh.
Additional information:
In the event that this is actually how it's supposed to work, it should certainly then be documented as such.
Mantis conversion info:
- Mantis ID: 35982
- OS: Windows
- OS Build: 10
- Build: Trunk
- Platform: x86_64
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 42773 (#33f6adfa)
- Monitored by: » Akira1364 (Akira1364)