Proper way to free an object returned in a function lazarus / 21-General ---------------------------------------------------------------------- rwebb616 · 2021-05-18 02:56 I have a function that returns a TStringList: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TData.GetCategories: TStringList;var  i : integer;begin  result := TStringlist.Create;  result.Sorted:=true;  result.Duplicates:=dupIgnore;  for i:=0 to Words.Count - 1 do    result.Add(Tword(words[i]).Category);end; I call it as such: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i listbox1.items := gamedata.getcategories heaptrc is complaining about getcategories at the line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i result.Add(Tword(words[i]).Category); First, is it the Tstringlist assigned to Result that is the issue?  and if so, how would I go about freeing it? ---------------------------------------------------------------------- Handoko · 2021-05-18 03:23 I usually will avoid creating new object as the function result. Instead I use procedure SomeProcess(out Data: TData); I remember there were a similar topic some months ago, that discussion is worth to read. ---------------------------------------------------------------------- Leledumbo · 2021-05-18 03:25 Quote from: rwebb616 on May 18, 2021, 04:56:27 amI call it as such: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i listbox1.items := gamedata.getcategories... First, is it the Tstringlist assigned to Result that is the issue?  and if so, how would I go about freeing it? Bad idea, use ListBox1.Items.Assign instead, assign the function result to a local variable first then free it after calling .Assign: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i lcats := gamedata.getcategories;with lcats do  try    listbox1.items.assign(lcats);  finally    lcats.Free;  end; Quote from: rwebb616 on May 18, 2021, 04:56:27 amheaptrc is complaining about getcategories at the line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i result.Add(Tword(words[i]).Category);Need to know what TWord.Category is first. If it's also a function, including property mapped one, then it's the same thing as above. ---------------------------------------------------------------------- engkin · 2021-05-18 03:26 This assignment: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     listbox1.items := gamedata.getcategories; Calls the property setter: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomListBox.SetItems(Value: TStrings);begin  if (Value <> FItems) then  begin    LockSelectionChange;    FItems.Assign(Value);    UnlockSelectionChange;  end;end; As you can see, it simply copies the items from the TStringList. Simply call Free after the assignment: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     categories:=gamedata.getcategories;    listbox1.items := categories;  categories.Free; A better way is to pass listbox1.items to gamedata.fillcategories. ---------------------------------------------------------------------- eny · 2021-05-21 11:55 Quote from: engkin on May 18, 2021, 05:26:37 amA better way is to pass listbox1.items to gamedata.fillcategories.+1 ---------------------------------------------------------------------- Warfley · 2021-05-21 12:41 Having classes as return types for functions is a bad idea, because it can make things very complicated. For example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function foo: TSomeClass;begin  Result := TSomeClass.Create;  Result.DoSomething;end; What happens if that DoSomething throws an exception? Well the instance reference is lost. So what you need to do is to always have a try-except block in such a function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function foo: TSomeClass;begin  Result := TSomeClass.Create;  try    Result.DoSomething;  except on e: Exception do    FreeAndNil(Result);    raise e;  end;end; If you simply pass that instance as a parameter: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure foo(inst: TSomeClass);begin  inst.DoSomething;end;This is not required, as you typically have a try-finally block on the callsite anyway: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i someInstance := TSomeClass.Create;try  foo(someInstance);  ...finally  someInstance.Free;end; So giving the instance as parameter makes it much easier because you don't need a try-except block, which is required if the instance is created by the function, and more importantly, it is also cleaner because creation and destruction happens in the same block/function