Hi,
I stumbled upon this bug (?).
Consider the following simple application.
unit main;
{$mode objfpc}{$H+}
interfaceuses
Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,
StdCtrls, LCLType, LCLProc;type
{ TForm1 }
TForm1 = class(TForm)
CreateBtn: TButton;
FreeBtn: TButton;
procedure CreateBtnClick(Sender: TObject);
procedure FreeBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
{ private declarations }
Ed: TSynEdit;
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Ed := nil;
KeyPreview := True;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift:
TShiftState);
begin
if (Key = VK_F4) and (Shift = [ssCtrl]) and Assigned(Ed) then
begin
Debugln('Ctrl+F4');
Key := 0;
Ed.Free;
Ed := nil;
end;
end;
procedure TForm1.CreateBtnClick(Sender: TObject);
begin
if Assigned(Ed) then Exit;
Ed := TSynEdit.Create(Self);
Ed.Align := alTop;
Ed.Parent := Self;
Ed.SetFocus;
end;
procedure TForm1.FreeBtnClick(Sender: TObject);
begin
if not Assigned(Ed) then Exit;
debugln('FreeBtnClick');
Ed.Free;
Ed := nil;
end;
end.It creates and frees a that pressing Ctrl-F4 will also free the TSynEdit.
If you run the program and press the Free button, the TSynEdit gets freed OK. If however you press Ctrl+F4 and at that moment the TSynEdit has focus, then you get a warning when freeing the TSynEdit:
WARNING: TLCLComponent.Destroy with LCLRefCount>0. Hint: Maybe the component is processing an event?
This happened to me in an app where I create TSynEdits derived control on a TTabSheet, both created at runtime on a custom TPageControl derived control.
It took me all afternoon to find out I hadn't screwed up the constructor/destructors of my own controls, and it only happened via the shortcut-key when the TSynEdit had focus.
Feauture or bug?
Bart
