Hello guys,
I have one problem. I have a simple project only with a Form1, Button1 and Label1. The idea is, when I press Button1, a function "getFileName" will be called. In this function, the openDialog will appear to let user to select a file name. Then the function result (the selected file name ) will be displayed on Lable1. This works; at least it return the file name selected by the user. Simple, right?
But I have "Heaptrc unit(check for mem-leaks)(-gh)" checked on. That is at "Project Option...>Compiler options>Debugging".
BTW: I use Lazarus IDE v1.6.2, win7, 64bit
When I compile and run the program and press Button1, I can select the file name from the dialog and display it on Label1. However when I close the program, the heaptrc gives that there is a leak. see picture. Finally I get SIGSEGV exception.
Here is my code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
Function getFileName:String;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption:= getFileName;
end;
Function getFileName:String;
var
openDialog : TOpenDialog;
selectedFileName:String;
begin
selectedFileName:='';
openDialog := TOpenDialog.Create(nil);
try
openDialog.Title:='Select a databse file'; // Title of Dialog
openDialog.InitialDir := 'C:\Users\Guest\Documents\'; // Starting directory
openDialog.Filter := 'Database files only|*.db'; // showonly files with .db extenstion
if openDialog.Execute then //Open dialog
selectedFileName:= OpenDialog.FileName;
finally
openDialog.Free;
end;
result:=selectedFileName;
end;
end