Einzelnen Beitrag anzeigen

Shark99

Registriert seit: 16. Mai 2007
403 Beiträge
 
#12

AW: Object.Free ruft unter XE5 kein Destroy mehr auf!?

  Alt 21. Dez 2016, 11:44
Im Code wird DataList (und nicht myDataList) freigegeben, war ein Vertipper hier im Forum (d.h. das Problem ist nach wie vor da).
Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections, Generics.Defaults;

type
  TTestForm = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TDataItem = class
    Modified: int64;
    Data: string;
    constructor Create(iModified: int64 = 0; sData: string = '');
  end;

  TDataList = class(TObjectList<TDataItem>)
  private
   StringList: TStringList;
  public
    Constructor Create(AOwnsObjects: Boolean = True);
    Destructor Destroy;
  end;

var
  TestForm: TTestForm;
  DataList: TDataList;

implementation

{$R *.dfm}

constructor TDataItem.Create(iModified: int64 = 0; sData: string = '');
begin
  Inherited Create;
  Modified:= iModified;
  Data := sData;
end;

constructor TDataList.Create(AOwnsObjects: Boolean = True);
begin
  Inherited Create(AOwnsObjects);

  StringList := TStringList.Create;
end;

destructor TDataList.Destroy;
begin
  StringList.Free;
  Inherited Destroy;
end;

procedure TTestForm.Button1Click(Sender: TObject);
var DataItem: TDataItem;
begin
{$IFDEF AUTOREFCOUNT}
  Caption := 'AUTOREFCOUNT ist definiert!';
{$ELSE}
  Caption := 'AUTOREFCOUNT ist nicht definiert!';
{$ENDIF}

  DataList := TDataList.Create(True);
  DataItem := TDataItem.Create(0 ,'Abc');
  DataList.Add(DataItem);
  Button1.Caption := DataList[0].Data;
  DataList.Free;
end;
end.
Das spuckt FastMM aus:
Delphi-Quellcode:

--------------------------------2016/12/21 11:42:31--------------------------------
A memory block has been leaked. The size is: 100

This block was allocated by thread 0xEE8, and the stack trace (return addresses) at the time was:
4068EE [System.pas][System][@GetMem$qqri][4316]
4076CF [System.pas][System][TObject.NewInstance$qqrv][15447]
407EBE [System.pas][System][@ClassCreate$qqrpvzc][16757]
4B5A3E [System.Classes.pas][System.Classes][Classes.TStringList.$bctr$qqrv][6821]
5BC1D1 [System.Generics.Collections.pas][Unit1][Generics.Collections.%TObjectList__1$p15Unit1.TDataItem%.$bctr$qqro][2083]
5BAAD8 [Unit1.pas][Unit1][TDataList.$bctr$qqro][52]
5BAB63 [Unit1.pas][Unit1][TTestForm.Button1Click$qqrp14System.TObject][70]
523A31 [Vcl.Controls.pas][Vcl.Controls][Controls.TControl.Click$qqrv][7340]
53A887 [Vcl.StdCtrls.pas][Vcl.StdCtrls][Stdctrls.TCustomButton.Click$qqrv][5313]
53B401 [Vcl.StdCtrls.pas][Vcl.StdCtrls][Stdctrls.TCustomButton.CNCommand$qqrr26Winapi.Messages.TWMCommand][5774]
5234D8 [Vcl.Controls.pas][Vcl.Controls][Controls.TControl.WndProc$qqrr24Winapi.Messages.TMessage][7224]

The block is currently used for an object of class: TStringList

The allocation number is: 704

Current memory dump of 256 bytes starting at pointer address 7FE7DEF0:
B4 25 49 00 00 00 00 00 00 00 00 00 A0 B8 E9 7F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 31 2B 6B 7C
80 80 80 80 80 80 80 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
´ % I . . . . . . . . . * ¸ é  . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 + k |
€ € € € € € € € . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

--------------------------------2016/12/21 11:42:31--------------------------------
This application has leaked memory. The small block leaks are (excluding expected leaks registered by pointer):

85 - 100 bytes: TStringList x 1

Note: Memory leak detail is logged to a text file in the same folder as this application. To disable this memory leak check, undefine "EnableMemoryLeakReporting".
Angehängte Dateien
Dateityp: rar GenericsMemTest.rar (2,85 MB, 1x aufgerufen)
  Mit Zitat antworten Zitat