Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi, Arrays returned by functions must be freed?

Posted on 2014-10-13
2
Medium Priority
?
455 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 35

Accepted Solution

by:
ste5an earned 2000 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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

718 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