Cocoa: Add autorelease pools.
authorMorten Johan Sørvig <morten.sorvig@nokia.com>
Mon, 12 Dec 2011 08:52:40 +0000 (09:52 +0100)
committerQt by Nokia <qt-info@nokia.com>
Mon, 12 Dec 2011 16:27:19 +0000 (17:27 +0100)
A couple of cases where we call Cococa APIs without
having an autorelease pool in place surfaced after
removing the global autorelease pool in 1a218a7.
(This happens when when Qt API is called before
app.exec() has started the Cocoa event loop.)

Add local autorelease pools to prevent memory leaks.

Change-Id: I0c4be3ff102aaff4539235857f95ab29fdbc9d70
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@nokia.com>
src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm
src/plugins/platforms/cocoa/qcocoabackingstore.mm
src/plugins/platforms/cocoa/qcocoaintegration.mm
src/plugins/platforms/cocoa/qcocoawindow.mm

index 6c3e403..9dbc60f 100644 (file)
@@ -159,6 +159,8 @@ static QString familyNameFromPostScriptName(QHash<QString, QString> &psNameToFam
 
 void QCoreTextFontDatabase::populateFontDatabase()
 {
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
     QCFType<CTFontCollectionRef> collection = CTFontCollectionCreateFromAvailableFonts(0);
     if (! collection)
         return;
@@ -243,8 +245,6 @@ void QCoreTextFontDatabase::populateFontDatabase()
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     NSArray *languages = [defaults stringArrayForKey: @"AppleLanguages"];
 
-    NSAutoreleasePool *pool = [NSAutoreleasePool new];
-
     NSDictionary *fallbackDict = [NSDictionary dictionaryWithContentsOfFile: @"/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreText.framework/Resources/DefaultFontFallbacks.plist"];
 
     for (NSString *style in [fallbackDict allKeys]) {
index 0cde196..d278392 100644 (file)
@@ -40,6 +40,7 @@
 ****************************************************************************/
 
 #include "qcocoabackingstore.h"
+#include "qcocoaautoreleasepool.h"
 
 #include <QtCore/qdebug.h>
 #include <QtGui/QPainter>
@@ -80,6 +81,7 @@ void QCocoaBackingStore::flush(QWindow *widget, const QRegion &region, const QPo
 {
     Q_UNUSED(widget);
     Q_UNUSED(offset);
+    QCocoaAutoReleasePool pool;
 
     QRect geo = region.boundingRect();
 
index 6d7770f..03348bb 100644 (file)
@@ -90,6 +90,8 @@ QCocoaIntegration::QCocoaIntegration()
     : mFontDb(new QCoreTextFontDatabase())
     , mEventDispatcher(new QCocoaEventDispatcher())
 {
+    QCocoaAutoReleasePool pool;
+
     qApp->setAttribute(Qt::AA_DontUseNativeMenuBar, false);
 
     NSApplication *cocoaApplication = [NSApplication sharedApplication];
index de38db5..86db8a5 100644 (file)
@@ -129,6 +129,7 @@ void QCocoaWindow::setGeometry(const QRect &rect)
 
 void QCocoaWindow::setVisible(bool visible)
 {
+    QCocoaAutoReleasePool pool;
     if (visible) {
         // The parent window might have moved while this window was hidden,
         // update the window geometry if there is a parent.
@@ -146,6 +147,8 @@ void QCocoaWindow::setVisible(bool visible)
 
 void QCocoaWindow::setWindowTitle(const QString &title)
 {
+    QCocoaAutoReleasePool pool;
+
     CFStringRef windowTitle = QCFString::toCFStringRef(title);
     [m_nsWindow setTitle: const_cast<NSString *>(reinterpret_cast<const NSString *>(windowTitle))];
     CFRelease(windowTitle);
@@ -164,6 +167,8 @@ void QCocoaWindow::lower()
 
 void QCocoaWindow::propagateSizeHints()
 {
+    QCocoaAutoReleasePool pool;
+
     [m_nsWindow setMinSize : qt_mac_toNSSize(window()->minimumSize())];
     [m_nsWindow setMaxSize : qt_mac_toNSSize(window()->maximumSize())];