Delphi Dynamic Array assign to temporary local dynamic array variable

591 Views Asked by At

I have problem with new released delphi 10.2 the new compiler show error when

var 
  FGlobalVar: array of integer;

procedure SomeProc()
var
  ALocalVar: array of integer;
begin 
  ALocalVar := Pointer(FGlobalVar); {assign dynamic array}
  {Do Something}
end;

In previous version compiler delphi not show any errors.

1

There are 1 best solutions below

0
On BEST ANSWER

That code should never have compiled, and Tokyo closes the loop hole. The problem with that cast is that reference counting can by bypassed. The code as you have it does not suffer that problem, but if the cast is written on the left hand side of the assignment, then no reference is taken.

Pointer(LocalVar) := GlobalVar;

Written this way round, LocalVar is assigned a reference to the dynamic array but the reference count is not incremented. I appreciate that your code is not written this way round, but I believe that this is the reason why the developers chose to make the change.

In any case, yhere is no need for the cast if you use types that are compatible. Switch to TArray<Integer> and the cast is not necessary. Further, your code will be able to interact with generic methods.