Cocoa: Clean up memory leaks and make autorelease pool only local to shrink down memory footprint

git-svn-id: trunk@44352 -
This commit is contained in:
freq 2014-03-05 11:23:47 +00:00
parent d23eff7453
commit f2556a023a
7 changed files with 50 additions and 16 deletions

View File

@ -506,8 +506,11 @@ type
{ TCocoaFont }
constructor TCocoaFont.CreateDefault(AGlobal: Boolean = False);
var Pool: NSAutoreleasePool;
begin
Pool := NSAutoreleasePool.alloc.init;
Create(NSFont.systemFontOfSize(0));
Pool.release;
end;
constructor TCocoaFont.Create(const ALogFont: TLogFont; AFontName: String; AGlobal: Boolean);
@ -587,13 +590,17 @@ begin
end;
constructor TCocoaFont.Create(const AFont: NSFont; AGlobal: Boolean);
var Pool: NSAutoreleasePool;
begin
inherited Create(AGlobal);
Pool := NSAutoreleasePool.alloc.init;
FFont := AFont;
FFont.retain;
FName := NSStringToString(FFont.familyName);
FSize := Round(FFont.pointSize);
FStyle := [];
FAntialiased := True;
Pool.release;
end;
class function TCocoaFont.CocoaFontWeightToWin32FontWeight(const CocoaFontWeight: Integer): Integer; static;
@ -710,6 +717,7 @@ constructor TCocoaBitmap.Create(AWidth, AHeight, ADepth, ABitsPerPixel: Integer;
var
HasAlpha: Boolean;
BitmapFormat: NSBitmapFormat;
pool:NSAutoReleasePool;
begin
inherited Create(False);
{$ifdef VerboseBitmaps}
@ -761,7 +769,9 @@ begin
// Create the associated NSImage
FImage := NSImage.alloc.initWithSize(NSMakeSize(AWidth, AHeight));
pool := NSAutoreleasePool.alloc.init;
Image.addRepresentation(Imagerep);
pool.release;
end;
constructor TCocoaBitmap.CreateDefault;
@ -1162,6 +1172,7 @@ begin
end;
procedure TCocoaBitmapContext.SetBitmap(const AValue: TCocoaBitmap);
var pool:NSAutoReleasePool;
begin
if Assigned(ctx) then
begin
@ -1172,9 +1183,11 @@ begin
if FBitmap <> nil then
begin
FBitmap := AValue;
pool:=NSAutoreleasePool.alloc.init;
ctx := NSGraphicsContext.graphicsContextWithBitmapImageRep(Bitmap.ImageRep);
ctx.retain; // extend live beyond NSAutoreleasePool
InitDraw(Bitmap.Width, Bitmap.Height);
pool.release;
end;
end;

View File

@ -60,8 +60,6 @@ type
TCocoaWidgetSet = class(TWidgetSet)
private
FTerminating: Boolean;
pool: NSAutoreleasePool;
FNSApp: NSApplication;
FCurrentCursor: HCursor;
FCaptureControl: HWND;

View File

@ -25,6 +25,7 @@
Initialize Carbon Widget Set
------------------------------------------------------------------------------}
procedure TCocoaWidgetSet.AppInit(var ScreenInfo: TScreenInfo);
var pool: NSAutoreleasePool;
begin
{$IFDEF VerboseObject}
DebugLn('TCocoaWidgetSet.AppInit');
@ -34,7 +35,9 @@ begin
{ Creates the application NSApp object }
FNsApp := NSApplication.sharedApplication;
pool := NSAutoreleasePool.alloc.init;
FNSApp.setDelegate(delegate);
pool.release;
end;
{------------------------------------------------------------------------------
@ -56,10 +59,13 @@ end;
procedure TCocoaWidgetSet.AppProcessMessages;
var
event: NSEvent;
pool:NSAutoReleasePool;
begin
pool := NSAutoreleasePool.alloc.init;
event := NSApp.nextEventMatchingMask_untilDate_inMode_dequeue(NSAnyEventMask, nil, NSDefaultRunLoopMode, true);
NSApp.sendEvent(event);
NSApp.updateWindows;
pool.release;
end;
{------------------------------------------------------------------------------
@ -70,10 +76,13 @@ end;
procedure TCocoaWidgetSet.AppWaitMessage;
var
event : NSEvent;
pool:NSAutoReleasePool;
begin
pool := NSAutoreleasePool.alloc.init;
event := NSApp.nextEventMatchingMask_untilDate_inMode_dequeue(NSAnyEventMask, NSDate.distantFuture, NSDefaultRunLoopMode, true);
NSApp.sendEvent(event);
NSApp.updateWindows;
pool.release;
end;
{------------------------------------------------------------------------------
@ -89,9 +98,6 @@ begin
FCurrentCursor:= 0;
FCaptureControl:= 0;
{ Creates the AutoreleasePool }
pool := NSAutoreleasePool.alloc.init;
NSMessageWnd := NSStringUTF8('HWND');
NSMessageMsg := NSStringUTF8('MSG');
NSMessageWParam := NSStringUTF8('WPARAM');
@ -119,21 +125,20 @@ destructor TCocoaWidgetSet.Destroy;
begin
inherited Destroy;
FreeStockItems;
ScreenContext.Free;
DefaultContext.Free;
DefaultBrush.Free;
DefaultPen.Free;
DefaultFont.Free;
DefaultBitmap.Free;
DefaultFont.Free;
DefaultPen.Free;
DefaultBrush.Free;
FreeSysColorBrushes;
FreeStockItems;
CocoaWidgetSet := nil;
{ Releases the AutoreleasePool }
pool.release;
end;
{------------------------------------------------------------------------------
@ -319,6 +324,7 @@ procedure TCocoaWidgetSet.InitStockItems;
var
LogBrush: TLogBrush;
logPen: TLogPen;
pool: NSAutoreleasePool;
begin
FillChar(LogBrush, SizeOf(TLogBrush),0);
LogBrush.lbStyle := BS_NULL;
@ -352,7 +358,9 @@ begin
FStockBlackPen := HPen(TCocoaPen.Create(LogPen, True));
FStockSystemFont := HFont(TCocoaFont.CreateDefault(True));
pool := NSAutoreleasePool.alloc.init;
FStockFixedFont := HFont(TCocoaFont.Create(NSFont.userFixedPitchFontOfSize(0), True));
pool.release;
end;
procedure TCocoaWidgetSet.FreeStockItems;
@ -377,8 +385,9 @@ begin
DeleteAndNilObject(FStockBlackPen);
DeleteAndNilObject(FStockWhitePen);
DeleteAndNilObject(FStockSystemFont);
DeleteAndNilObject(FStockFixedFont);
DeleteAndNilObject(FStockSystemFont);
end;
procedure TCocoaWidgetSet.FreeSysColorBrushes;

View File

@ -1906,7 +1906,6 @@ begin
h:=lclGetTopBarHeight;
ns.size.height:=ns.size.height+h;
ns.origin.y:=ns.origin.y-h;
setFrame_display(ns, isVisible);
end;

View File

@ -1045,12 +1045,13 @@ var
window, windowbelowpoint: NSWindow;
p:NSPoint;
winnr:NSInteger;
pool:NSAutoReleasePool;
begin
Result := 0;
if not assigned(NSApp) then
Exit;
pool := NSAutoreleasePool.alloc.init;
windows := NSApp.windows;
for win := 0 to windows.count - 1 do
begin
@ -1066,6 +1067,7 @@ begin
exit;
end;
end;
pool.release;
end;
@ -1810,10 +1812,12 @@ end;
function TCocoaWidgetSet.SetForegroundWindow(HWnd: HWND): boolean;
var
Obj: NSObject;
pool: NSAutoreleasePool;
begin
Result := HWnd <> 0;
if Result then
begin
pool := NSAutoreleasePool.alloc.init;
NSApp.activateIgnoringOtherApps(True);
Obj := NSObject(HWnd);
if Obj.isKindOfClass(NSWindow) then
@ -1823,6 +1827,7 @@ begin
NSView(Obj).window.makeKeyAndOrderFront(NSApp)
else
Result := False;
pool.release;
end;
end;

View File

@ -1272,9 +1272,14 @@ begin
end;
class procedure TCocoaWSWinControl.ShowHide(const AWinControl: TWinControl);
var pool: NSAutoreleasePool; // called outside apploop on startup - therefore has to be enframed by pool
begin
if AWinControl.HandleAllocated then
begin
pool := NSAutoreleasePool.alloc.init;
NSObject(AWinControl.Handle).lclSetVisible(AWinControl.HandleObjectShouldBeVisible);
pool.release;
end;
end;
class procedure TCocoaWSWinControl.Invalidate(const AWinControl: TWinControl);

View File

@ -385,12 +385,13 @@ var
cnt: TCocoaWindowContent;
ns: NSString;
R: NSRect;
pool:NSAutoReleasePool;
begin
//todo: create TCocoaWindow or TCocoaPanel depending on the border style
// if parent is specified neither Window nor Panel needs to be created
// the only thing that needs to be created is Content
pool := NSAutoreleasePool.alloc.init;
R := CreateParamsToNSRect(AParams);
cnt := TCocoaWindowContent.alloc.initWithFrame(R);
@ -444,6 +445,7 @@ var
end;
end;
pool.release;
Result := TLCLIntfHandle(cnt);
end;
@ -605,12 +607,15 @@ end;
class procedure TCocoaWSCustomForm.SetBounds(const AWinControl: TWinControl;
const ALeft, ATop, AWidth, AHeight: Integer);
var pool: NSAutoreleasePool;
begin
if AWinControl.HandleAllocated then
begin
pool := NSAutoreleasePool.alloc.init;
//debugln('TCocoaWSCustomForm.SetBounds: '+AWinControl.Name+'Bounds='+dbgs(Bounds(ALeft, ATop, AWidth, AHeight)));
NSObject(AWinControl.Handle).lclSetFrame(Bounds(ALeft, ATop, AWidth, AHeight));
TCocoaWindowContent(AwinControl.Handle).callback.boundsDidChange;
pool.release;
end;
end;