Solved

Delphi, Arrays returned by functions must be freed?

Posted on 2014-10-13
2
379 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 33

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 167
Making delphi communicate with a c# service 16 122
Delphi: Connect to running MS Outlook 4 163
Delphi: barcode reading on android platform 1 72
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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