Is it memory safe to provide an object as a function result?

后端 未结 8 2062
旧巷少年郎
旧巷少年郎 2021-01-06 21:05

Here I provide simple piece of code.

function GetStringList:TStringList;
var i:integer;
begin
   Result:=TStringList.Create;
   Result.Add(\'Adam\');
   Res         


        
8条回答
  •  太阳男子
    2021-01-06 21:42

    I don't know what you mean by safe, but it is common practice. The caller of the function becomes responsible for freeing the returned object:

    var
       s : TStringList; 
    begin
       s := GetStringList;
       // stuff
       s.free;
    end;
    

提交回复
热议问题