Cocoa: MainPool is divided into two stages, covering the whole cycle, fix resources leak

This commit is contained in:
rich2014 2023-08-26 23:56:18 +08:00
parent 7f00662152
commit b65ca17223
2 changed files with 20 additions and 19 deletions

View File

@ -299,6 +299,22 @@ const
// Lack of documentation, provisional definition
LazarusApplicationDefinedSubtypeWakeup = 13579;
procedure InternalInit;
begin
// MacOSX 10.6 reports a lot of warnings during initialization process
// adding the autorelease pool for the whole Cocoa widgetset
MainPool := NSAutoreleasePool.alloc.init;
end;
procedure InternalFinal;
begin
if Assigned(MainPool) then
begin
MainPool.release;
MainPool := nil;
end;
end;
procedure wakeupEventLoop;
var
ev: NSevent;
@ -479,6 +495,8 @@ end;
{$ifdef COCOALOOPOVERRIDE}
procedure TCocoaApplication.run;
begin
InternalFinal; // MainPool Stage 1 final
InternalInit; // MainPool Stage 2 init
{$ifdef COCOAPPRUNNING_SETINTPROPERTY}
setValue_forKey(NSNumber.numberWithBool(true), NSSTR('_running'));
{$endif}
@ -746,22 +764,6 @@ begin
end;
{$endif}
procedure InternalInit;
begin
// MacOSX 10.6 reports a lot of warnings during initialization process
// adding the autorelease pool for the whole Cocoa widgetset
MainPool := NSAutoreleasePool.alloc.init;
end;
procedure InternalFinal;
begin
if Assigned(MainPool) then
begin
MainPool.release;
MainPool := nil;
end;
end;
type
AppClassMethod = objccategory external (NSObject)
function sharedApplication: NSApplication; message 'sharedApplication';
@ -942,9 +944,10 @@ begin
end;
initialization
InternalInit; // MainPool Stage 1 init
// {$I Cocoaimages.lrs}
finalization
InternalFinal;
InternalFinal; // MainPool Stage 2 Final
end.

View File

@ -31,8 +31,6 @@ begin
{$IFDEF VerboseObject}
DebugLn('TCocoaWidgetSet.AppInit');
{$ENDIF}
InternalInit;
WakeMainThread := @OnWakeMainThread;
ScreenInfo.PixelsPerInchX := CocoaBasePPI;
ScreenInfo.PixelsPerInchY := CocoaBasePPI;