Is this actually a memory leak, am I looking at this all wrong?
There should be no memory leak, although the call to
FreeAndNil is superfluous, as VTwin said.
I woud only do a little change (a matter of personal preference): I wouldn't create the dialog unitl I needed. That is, I would move the dialog-creation code from
FormCreate() fo
SelectImagesButtonClick, in add a guard against multi-creation:
procedure TImportImagesForm.SelectImagesButtonClick(Sender: TObject);
var
i: integer;
fileName: string;
begin
if not Assigned(SelectImportImagesDialog) the begin
SelectImportImagesDialog := TOpenDalog.Create(Self);
{... rest of dialog initialization ...}
end;
if SelectImportImagesDialog.Execute then
begin
numberofFiles := SelectImportImagesDialog.Files.Count;
SetImportButtonState;
end;
end;
I understand, but in this case the dialog should be created, opened and released in the same routine, for example:
That's what I do usually (normally using a function to create and setup the dialog, if needed).
But sometimes you need the dialog to keep its current properties as they are (size/position, InitialDir, FileName, etc). In those cases it's best to use a global dialog object (e.g. as a field of the form) created on demand and not freed until program close-down.