Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: two fields at same memory location - compiler bug?

I tried this in Delphi XE SP 1, see comment in code. Never tried in newer revisions, have not installed them now, is somebody familiar with this bug? Didn't find anything in their QC either...

unit Testing;

interface

uses
  Generics.Collections;

type
  TBaseObjectList<T: class> = class(TObjectList<T>)
  private
    FUpdateLock: Integer;
  public
    constructor Create; virtual;
    procedure LockUpdate;
    procedure UnlockUpdate;
    function UpdateUnlocked: Boolean;
    property UpdateLock: Integer read FUpdateLock;
  end;

  TAdvObject = class(TObject)
  end;

  TAdvObjectList = class(TBaseObjectList<TAdvObject>)
  private
    FHelper: Integer;
  public
    constructor Create;
    property Helper: Integer read Fhelper;
  end;

implementation

{ TBaseObjectList<T> }

constructor TBaseObjectList<T>.Create;
begin
  inherited Create;
  FUpdateLock := 0;
end;

procedure TBaseObjectList<T>.LockUpdate;
begin
  Inc(FUpdateLock);
end;

procedure TBaseObjectList<T>.UnlockUpdate;
begin
  if FUpdateLock > 0 then
    Dec(FUpdateLock);
end;

function TBaseObjectList<T>.UpdateUnlocked: Boolean;
begin
  Result := FUpdateLock = 0;
end;

{ TAdvObjectList }

constructor TAdvObjectList.Create;
begin
  LockUpdate;
  try
    // this increments FUpdateLock as well because FHelper and FUpdateLock are mapped to same memory location, it can be seen in debugger watches, it seems to me to be a bug
    Inc(FHelper);
  finally
    UnlockUpdate;
  end;
end;

begin
  TAdvObjectList.Create;
end.

Thank you TK

like image 711
tk_ Avatar asked May 26 '15 12:05

tk_


1 Answers

Just copying answer from the comments above: qc.embarcadero.com/wc/qcmain.aspx?d=101308 Bug resolved in Delphi XE4.

like image 139
tk_ Avatar answered Sep 21 '22 10:09

tk_