Board index » delphi » Access violation with dynamic array function

Access violation with dynamic array function

I'm trying to write a function ( in a custom VCL component) that returns
a dynamic integer array. The component isn't installed and I'm testing
it from a test application. I can successfully call the function but
when I close my test application I get an access violation error. (The
component is fine without this function).

Here is the code from the unit containg the custom component, followed
by code from the calling application. I'm fairly new to this so I expect
my mistake is trivial. Or maybe this isnt the best way of doing it
anyway. Any suggestions much appreciated.

TIntArray = array of integer;
..........................................
function TadhImageL.GetRectX: TIntArray;
var temparray : TIntArray;
    i : integer;
    temprect : TRectObject;
begin
  SetLength(Temparray, RectList.count);
  TempRect := TRectObject.Create;
  for i := 0 to RectList.Count-1 do
  begin
    TempRect := TRectObject(RectList[i]);
    Temparray[i] := TempRect.X;
  end;
  Result := TempArray;
  TempRect.Free;

end;
(From calling application)
procedure TForm1.GetXBtnClick(Sender: TObject);
var XArray : TIntArray;
    num : integer;
begin
    SetLength(XArray, MyImage.GetNumObjects);
    XArray := MyImage.GetRectX;
    for num := 0 to MyImage.GetNumObjects-1 do
    listbox1.Items.add(IntToStr(XArray[num]));
end;

 

Re:Access violation with dynamic array function


Hi,

just look into the code. I think I found your problem.

Quote
Alan Hale <adh...@clara.net> wrote:
> I'm trying to write a function ( in a custom VCL component) that returns
> a dynamic integer array. The component isn't installed and I'm testing
> it from a test application. I can successfully call the function but
> when I close my test application I get an access violation error. (The
> component is fine without this function).
> Here is the code from the unit containg the custom component, followed
> by code from the calling application. I'm fairly new to this so I expect
> my mistake is trivial. Or maybe this isnt the best way of doing it
> anyway. Any suggestions much appreciated.
> TIntArray = array of integer;
> ..........................................
> function TadhImageL.GetRectX: TIntArray;
> var temparray : TIntArray;
>     i : integer;
>     temprect : TRectObject;
> begin
>   SetLength(Temparray, RectList.count);

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here you create an array of integers the first time.

Quote
>   TempRect := TRectObject.Create;
>   for i := 0 to RectList.Count-1 do
>   begin
>     TempRect := TRectObject(RectList[i]);
>     Temparray[i] := TempRect.X;
>   end;
>   Result := TempArray;
>   TempRect.Free;
> end;
> (From calling application)
> procedure TForm1.GetXBtnClick(Sender: TObject);
> var XArray : TIntArray;
>     num : integer;
> begin
>     SetLength(XArray, MyImage.GetNumObjects);

      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here you create an array of integers the second time. Then you assign the
XArray value which is already a defined array a new defined array from your
method. So the first array is somewhere in the memory. When you close your
application and free the XArray value then its gone. If you try to do the
same in your class then the array is already gone and there is an error.
Otherwise there is still an unfreed array of integers from the line above
and that should not be the case.

Quote
>     XArray := MyImage.GetRectX;
>     for num := 0 to MyImage.GetNumObjects-1 do
>     listbox1.Items.add(IntToStr(XArray[num]));
> end;

Hope it helps.

cu,
Toralf

Other Threads