From cd6227e4c989cc92ec873c042c4fc596de0f0dc3 Mon Sep 17 00:00:00 2001 From: freq Date: Fri, 27 Dec 2013 10:47:23 +0000 Subject: [PATCH] Fix memory leaks on hint windows and popup menu git-svn-id: trunk@43598 - --- lcl/interfaces/cocoa/cocoaprivate.pp | 10 ++++++++-- lcl/interfaces/cocoa/cocoawsforms.pp | 28 +++++++++++++++++++++++++++ lcl/interfaces/cocoa/cocoawsmenus.pas | 19 ++++++++++++++++-- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/lcl/interfaces/cocoa/cocoaprivate.pp b/lcl/interfaces/cocoa/cocoaprivate.pp index 37fd3bcdf7..68e9a3f4f6 100644 --- a/lcl/interfaces/cocoa/cocoaprivate.pp +++ b/lcl/interfaces/cocoa/cocoaprivate.pp @@ -175,8 +175,9 @@ type TCocoaMenuItem = objcclass(NSMenuItem) public - callback: IMenuItemCallback; + menuItemCallback: IMenuItemCallback; procedure lclItemSelected(sender: id); message 'lclItemSelected:'; + function lclGetCallback: IMenuItemCallback; override; end; { TCocoaButton } @@ -1934,7 +1935,12 @@ end; procedure TCocoaMenuItem.lclItemSelected(sender:id); begin - callback.ItemSelected; + menuItemCallback.ItemSelected; +end; + +function TCocoaMenuItem.lclGetCallback: IMenuItemCallback; +begin + result:=menuItemCallback; end; end. diff --git a/lcl/interfaces/cocoa/cocoawsforms.pp b/lcl/interfaces/cocoa/cocoawsforms.pp index 2cbf3ac151..dd9bd72bcc 100644 --- a/lcl/interfaces/cocoa/cocoawsforms.pp +++ b/lcl/interfaces/cocoa/cocoawsforms.pp @@ -131,6 +131,7 @@ type public published class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override; + class procedure DestroyHandle(const AWinControl: TWinControl); override; end; { TCocoaWSScreen } @@ -210,6 +211,33 @@ begin Result := TLCLIntfHandle(cnt); end; + +class procedure TCocoaWSHintWindow.DestroyHandle(const AWinControl: TWinControl); +var + cnt: TCocoaWindowContent; + Callback: ICommonCallback; + CallbackObject: TObject; +begin + if not AWinControl.HandleAllocated then + Exit; + + cnt:= TCocoaWindowContent(AWinControl.Handle); + cnt.removeFromSuperview; + + cnt.ownwin.close; + + Callback := cnt.lclGetCallback; + if Assigned(Callback) then + begin + CallbackObject := Callback.GetCallbackObject; + Callback := nil; + cnt.lclClearCallback; + CallbackObject.Free; + end; + + cnt.release; +end; + { TLCLWindowCallback } function TLCLWindowCallback.CanActivate: Boolean; diff --git a/lcl/interfaces/cocoa/cocoawsmenus.pas b/lcl/interfaces/cocoa/cocoawsmenus.pas index 23ca6770c3..dcc600be2b 100644 --- a/lcl/interfaces/cocoa/cocoawsmenus.pas +++ b/lcl/interfaces/cocoa/cocoawsmenus.pas @@ -170,7 +170,7 @@ begin objcselector('lclItemSelected:'), NSString.alloc.init); ns.release; item.setTarget(item); - TCocoaMenuItem(item).callback:=TLCLMenuItemCallback.Create(item, AMenuItem); + TCocoaMenuItem(item).menuItemCallback:=TLCLMenuItemCallback.Create(item, AMenuItem); item.setEnabled(AMenuItem.Enabled); end; @@ -184,8 +184,23 @@ end; Destroys menu item in Cocoa interface ------------------------------------------------------------------------------} class procedure TCocoaWSMenuItem.DestroyHandle(const AMenuItem: TMenuItem); +var + callback: IMenuItemCallback; + callbackObject: TObject; + item : TCocoaMenuItem; begin - + if AMenuItem.Caption <> '-' then + begin + item:=TCocoaMenuItem(AMenuItem.Handle); + callback := item.lclGetCallback; + if Assigned(callback) then + begin + callbackObject := callback.GetCallbackObject; + callback := nil; + item.lclClearCallback; + callbackObject.Free; + end; + end; end; {------------------------------------------------------------------------------