Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Code Generation/Optimization    [ Add a report in this area ]  
Report #:  118135   Status: Closed
[iOS] Memory leak with write property under ARC
Project:  Delphi Build #:  18.0.4905.60485
Version:    18.1 Submitted By:   Yasuhiro Kudo
Report Type:  Crash / Data loss / Total failure Date Reported:  8/22/2013 3:50:50 PM
Severity:    Serious / Highly visible problem Last Updated: 4/15/2014 6:42:03 PM
Platform:    Apple mobile OS Internal Tracking #:   42486
Resolution: Fixed (Resolution Comments) Resolved in Build: : XE6
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: 10
Description
In the following source code, the TObject object is not freed.
I don't know the reason, but it seems to be caused this issue by that the Obj.RefCount is 0 in the SetObj procedure.

This issue is similar to QC#75036, but it might be different issue because it occurs in the case of using property with write specifier.
Steps to Reproduce:
type
  TLeakTest = record
    FInt: Integer;
    procedure SetObj(const Obj: TObject);
    property Obj: TObject write SetObj;
  end;

procedure TLeakTest.SetObj(const Obj: TObject);
begin
  FInt := Obj.RefCount;
end;

var
  LeakTest: TLeakTest;
  I: Integer;
begin
  LeakTest.Obj := TObject.Create;
  I := LeakTest.FInt;   //-> Returns 0
end;
Workarounds
Be able to avoid this issue as follow.

var
  Obj: TObject;
  LeakTest: TLeakTest;
  I: Integer;
begin
  Obj := TObject.Create;
  LeakTest.Obj := Obj;
  I := LeakTest.FInt;   //-> Returns 1
end;
Attachment
None
Comments

Tomohiro Takahashi at 8/22/2013 7:34:06 PM -
In fact, similar issue was reported in QC as below.
For example, creating instacnes without assigning to variable will not destroy and produce memory leaks.
We hope the issue will be fixed in next release...
-----------
type
  TMyObject = class (TObject)
  private
    MyData: TBytes;
  public
    constructor Create; virtual;
  end;
...
...
constructor TMyObject.Create;
begin
inherited;
SetLength(MyData,1024*1024*50);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to 4 do
  begin
    TMyObject.Create;
  end;
end;
-----------

wenjie zhou at 9/10/2013 7:54:37 PM -
I have report a bug QC103951 before. Now resolution is  As Design.

This report has the same workaround with QC103951.

Tomohiro Takahashi at 8/22/2013 7:37:49 PM -
and, I think this code will cause memory leaks.
--------------
begin
  with TMyObject.Create do
  begin
    ;
  end;
end;
--------------

Alysson Cunha at 9/9/2013 4:39:12 AM -
There's another workaround:

    procedure SetObj(const Obj: TObject); ... remove the "const" modifier:
    procedure SetObj(Obj: TObject);

Now the Delphi compiler will generate increment/decrement of the reference count inside the SetObj method, avoiding memory leak.

The same issue happens with Interfaces with Desktop compiler.

Server Response from: ETNACODE01