Solved

Delphi, Arrays returned by functions must be freed?

Posted on 2014-10-13
2
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month5 days, 23 hours left to enroll

627 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