From: Dmitry D. Chernov Date: Tue, 19 Sep 2023 16:45:12 +0000 (+1000) Subject: Fix LCLRefCount warning and possible use-after-free X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=09c7e2fe60e25c431d1cea07282f471b8309d2c9;p=d2df-editor.git Fix LCLRefCount warning and possible use-after-free Verbatim warning text: "WARNING: TMenuItem.Destroy with LCLRefCount>0. Hint: Maybe the component is processing an event?". This was caused by a complex call chain of miRecentFileExecute() -> OpenMap() -> RefreshRecentMenu() -> RefillRecentMenu(), which result in the sender menu item being removed. In such cases, Application.ReleaseComponent() should be used. https://forum.lazarus.freepascal.org/index.php?topic=33591.0 - Topic: [SOLVED] How to delete a button on an OnClick event https://www.lazarusforum.de/viewtopic.php?p=118333 - [gelöst]Was tun bei: Maybe the component is processing an event? --- diff --git a/src/editor/f_main.pas b/src/editor/f_main.pas index e4e0643..e2fdc20 100644 --- a/src/editor/f_main.pas +++ b/src/editor/f_main.pas @@ -2554,8 +2554,10 @@ end; procedure TMainForm.miRecentFileExecute (Sender: TObject); var s, fn: AnsiString; + n: LongInt; begin - s := RecentFiles[(Sender as TMenuItem).Tag]; + n := (Sender as TMenuItem).Tag; + s := RecentFiles[n]; fn := g_ExtractWadName(s); if FileExists(fn) then OpenMap(fn, g_ExtractFilePathName(s)) @@ -2585,7 +2587,7 @@ begin else begin menu.Delete(i); - MI.Destroy(); + Application.ReleaseComponent(MI); end; end;