Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/RTL/Delphi/Null-terminated strings    [ Add a report in this area ]  
Report #:  24437   Status: Closed
memory leak using string in record type
Project:  Delphi Build #:  10.0.2163.24987
Version:    10.0 Submitted By:   Renzo Fallani
Report Type:  Basic functionality failure Date Reported:  2/2/2006 12:59:52 PM
Severity:    Critical / Show Stopper Last Updated: 2/16/2006 4:41:46 PM
Platform:    All versions Internal Tracking #:  
Resolution: Test Case Error (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (1 Total Rating)
1.00 out of 5
Total Votes: None
Description
(sorry for my english)

FastMM report memory leak for the code described in "Steps".

If you change the declaration of TMyRec as:

TMyRec = record
   aStr: WideString;
end;

declaring aStr as WideString (instead of as string), no memory leaks are reported.


Steps to Reproduce:
Create a new Win32 project with a form.
insert the code below (declaration of TMyRec, FormCreate event handler).
Execute in the IDE
Close the application.
You get a message of memory leak for every TmyRec allocated.

-----------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
PMyRec = ^TMyRec;
TMyRec = record
   aStr: string;
end;


procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
frec : PMyRec;
  fList : TList;
begin
ReportMemoryLeaksOnShutdown := True;
fList := TList.Create;
for i  := 0 to 1000 do
  begin
   new(fRec);
   fRec^.aStr := 'test';
   fList.Add(fRec);
  end;

for i  := 0 to fList.Count-1 do
  begin
   Dispose(fList[i]);
  end;
fList.Free;
end;


end.


Workarounds
..
Attachment
None
Comments

Michael Winter at 2/10/2006 12:43:46 AM -
No bug, the memory leak caused by you.

The compiler does have no chance to see the pointer you put into the Dispose function is a PMyRec. Therefore it can't create code to finalize the record before it's disposed.

To fix the problem, use

  Dispose(PMyRec(fList[i]));

instead.

Using WideString doesn't really fix your case, it's only that FastMM isn't resonsible for WideString memory. Your application still leaks.

Renzo Fallani at 2/11/2006 8:08:39 AM -
Yes, I agree with you. I have confused because of an other bug in a large project, and the different behaviour of string and widestring.

Regards.

Server Response from: ETNACODE01