Below is the thought of Freeing Objects in Delphi's TStrings Items by Zarko Gajic at at I am using Delphi 7, TStringList does not have OwnsObjects. Running the following code will prompt EaccessViolation error.

Board index » delphi » List of integers? Delphi Developer. I would like to have a list of integers and currently see my. With TStringList. Remember that a Set declaration is a variable. As such, it has all possible values set off at the start. When you initialise the set, you will be setting some or all.

I donot know why and how to walk around it to free objects. Thanks a lot. Procedure TForm6.freelist(const alist: TStringList); var i: integer; begin try for i:=0 to pred(alist.Count) do begin if Assigned(alist.Objects[i]) then begin alist.Objects[i].Free; alist.objects[i]:=nil; end; end; alist.Clear; finally alist.Free; end; end; Edit: I add this line, alist.Objects[i]:=Pointer(0); and there is no error. For i:=0 to pred(alist.Count) do begin if Assigned(alist.Objects[i]) then begin alist.Objects[i]:=Pointer(0); //newly added line. Alist.Objects[i].Free; alist.objects[i]:=nil; end; end. //But I do not know if this is correct and // if the efficiency will be compromised.
//This is awkward method? The original answer below answers the question you asked. However, it transpires in the comments that you are not adding objects to your string list. You are simply adding integers cast to Pointer. In that case you must not call Free on those integers and that would completely explain your error.
So the entire code in your question is gratuitous and you should simply call Free on the list when you are done with it. When you cast an integer to a pointer and add it to the list, there is no memory allocated beyond that used to store the pointer. That is because an integer is a value type. When you add a true pointer or an object then you are adding a reference type and disposal of that object involves calling Free for an object or FreeMem (or Dispose) for a pointer. Original answer to the question as asked Your original code is correct, albeit a little clunky. The problem you have is in the code that populates Objects[]. Since we cannot see that code we can't say what you have got wrong.
Now, having said your code was clunky, here's how I would write it: procedure ClearList(List: TStringList); var i: Integer; begin for i:= 0 to pred(List.Count) do List.Objects[i].Free; List.Clear; end; Some notes on the above: • You do not need the if Assigned(obj) test before calling obj. Descargar Reggaeton Brasilero more. Free. I explain why not here: • There's little point in setting the items to nil if you are about to call Clear. • You should not call List.Free in such a routine. Your list's life time should be managed separately from the code that clears the list. The call to List.Free should be made in the same scope as the call that constructs the list.
Or, if this list is a field of a class, then the call to List.Free should be made from the destructor of the owning class. Now, as I have already said in comments to this question and your previous question, all this lifetime management work with the Objects[] property of a Delphi 7 string list is very unsatisfactory. It's all too easy to leak objects. I would recommend the following alternatives: • Switch to using TObjectList rather than TStringList.