Solved

Delphi, Arrays returned by functions must be freed?

Posted on 2014-10-13
2
303 Views
Last Modified: 2014-10-13
Hello,
I've got a doubt about the need of freeing arrays returned by functions.
Here's my array type and function:

type
  TStringArray = array of string; 

function GetHelloStringArray() : TStringArray;
begin
     SetLength(Result,5);
     Result[0] := 'H';
     Result[1] := 'E';
     Result[2] := 'L';
     Result[3] := 'L';
     Result[4] := 'O';
end;

Open in new window


Suppose I'm using the GetHelloStringArray function as shown here:

procedure TForm1.Button1Click(Sender: TObject);
var
  MyArray : TStringArray;
begin
  MyArray := GetHelloStringArray();
  //DO SOMETHING HERE
  //...
  SetLength(MyArray,0) // <--- IS THIS NEEDED?
end;

Open in new window


Is the SetLength command needed or can be avoided?
Thanks to everyone.
0
Comment
Question by:ExDev001
2 Comments
 
LVL 32

Accepted Solution

by:
ste5an earned 500 total points
ID: 40376707
Ask the oracle itself (Delphi) :)

program Project9;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  TStringArray = array of string;

function GetHelloStringArray(): TStringArray;
begin
  SetLength(Result, 5);
  Result[0] := 'H';
  Result[1] := 'E';
  Result[2] := 'L';
  Result[3] := 'L';
  Result[4] := 'O';
end;

var
  MyArray: TStringArray;

begin
  ReportMemoryLeaksOnShutdown := True;
  MyArray := GetHelloStringArray();
  WriteLn('Press any key...');
  ReadLn;

end.

Open in new window


MyArray is a normal data type, thus you don't need to do anything. I would be different, when this is a reference type, where only the reference is managed by Delphi, but not the object or memory where the references points to.
0
 

Author Closing Comment

by:ExDev001
ID: 40377212
Thanks alot for clarification!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now