When you call a function, do you need to release what you created in it or functions are done by everyone?
For example:
TPlayer = class type TSomeType = class class function myFunc(str: string): TPlayer; end; class function TSomeType.myFunc(str: string): TPlayer; var Player: TPlayer; begin Player.Param1 := 'qwer'; Player.Param2 := 3; Player.Param3.SubParam1 := TStringList.Create; Player.Param3.SubParam1.DelimitedText := 'qwe,asd,zxc'; Player.Param3.SubParam2 := False; Player.Param4. := TStringList.Create; Player.Param4.DelimitedText := 'rty,fgh,vbn'; Result := Player; end; Player := TSomeType.myFunc(str); Is it possible and necessary, after Result := Player; set Player := nil; ?
Or it is necessary to free each StringList separately? Or both?
If from the function, call another one and assign the result to a variable, should this variable also be released?
UPD
unit myTypes; interface uses System.SysUtils, System.Classes, ExtCtrls, IdContext, Generics.Collections, Generics.Defaults, SyncObjs, IdHashMessageDigest; ... type TPlayer = class Name: String; Pass: String; Active: Boolean; Authorized: Boolean; Balance: Integer; Game: TPlayerGame; Oper: TPlayerParent; Admin: TPlayerParent; SuperAdmin: TPlayerParent; Session: String; TCPAddress: TIdContext; WSAddress: String; HttpAddress: String; HostWS: String; HostTcp: String; HostHttp: String; constructor Create(pName: String); destructor Destroy; override; end; implementation ... { TPlayer } constructor TPlayer.Create(pName: String); begin //Inherited; self.Name := pName; self.Pass := ''; self.Active := False; self.Authorized := False; self.Balance := 0; self.Game := TPlayerGame.Create; self.Oper := TPlayerParent.Create; self.Admin := TPlayerParent.Create; self.SuperAdmin := TPlayerParent.Create; Self.Session := ''; self.TCPAddress := nil; self.WSAddress := ''; self.HttpAddress := ''; self.HostWS := ''; self.HostTcp := ''; self.HostHttp := ''; end; destructor TPlayer.Destroy; begin self.Game.Destroy; self.Oper.Destroy; self.Admin.Destroy; self.SuperAdmin.Destroy; self.TCPAddress := nil; Inherited; end; end. Here are the scenarios (I tried to describe briefly so do not look for logic in the actions):
function Authorizetion(login, pass): Boolean; var Player: TPlayer; begin // если авторизация удачна if Authorized then begin // создаю объект Player := TPlayer.Create(login); // добавляю объект в список TmyServer.SetPlayer(Player.Name, Player); // TDictionary.AddOrSetValue // я создал Player , потом добавил в TDictionary его копию, // а тот что в этой функцие, уничтожаю Player.Destroy; end; end; function LoadFromDateBase(name: String): TPlayer; var Player: TPlayer; begin // беру Player из TDictionary Player := TmyServer.GetPlayer(pName); // меняю некоторые значения на прочтенные из базы Player := FDQuaery.FieldByName().AsString; Result := Player; // тут я сохранил Player в Result(тот объект который вызвал эту функцию), // а теперь его уничтожаю Player.Destroy; end;