The problem is that if x = '' , finally all the same. Since field_list initialized only when x <> '' , this is a random memory cell before this point, because it is an uninitialized local variable. A random value allows you to call field_list.free , because it is not nil . (Delphi does not initialize local variables (declared inside a function or procedure).
var somevar: sometype; begin // at this point, somevar is just a chunk of memory that // holds whatever happens to be in that chunk somevar := nil; // now somevar = a specific value you can test // other code end;
You do not need to test <> nil (as others pointed out in the comments) if you structure your code correctly.
procedure TfrmXQuery.FieldListFillFromDefault; var field_list : TStringList; begin if x <> '' then begin field_list := TStringList.Create; try {do some stuff with field_list} finally field_list.Free; end; end; end;
(If you included hints and warnings, the compiler would tell you that field_list may not have been initialized , which would help you solve this yourself.)
source share