Link to home
Start Free TrialLog in
Avatar of Hamlet081299
Hamlet081299

asked on

WARNING: Passing 0 length variant arrays may be hazardous to your health!!!

I found an interesting quirk in Delphi (v5).

The following piece of code causes a rather nasty access violation if Args is of 0 length.
---
function Y(const Name: String; const Args: array of variant): variant;
begin
  Result := Length(Args);
end;

function X(const Name: String; Args: array of variant): variant;
begin
  Result := Y(Name, Args);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Args: array of variant;
  ret: variant;
begin
  SetLength(Args, 0);
  ret := X('test', Args);
  ShowMessage(ret);
end;
---

This is just a warning to those who may be interested.  The solution is quite simple make Args in function X be "const Args: ..." (like in Y)

However I still contend that the code is perfectly valid.

If someone thinks that my code is flawed (rather than Delphi that is) then I may be willing to dish out some points.

;-)
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Madshi
Madshi

P.S: I think I did submit a bug report for this already after D4 came out...
Avatar of Hamlet081299

ASKER

Sorry there's no points on this one, but I thought it better to have an accepted answer rather than delete the question, so that people may refer to it.