diff --git a/debian/copyright b/debian/copyright index 560c27a86..375016791 100644 --- a/debian/copyright +++ b/debian/copyright @@ -47,6 +47,7 @@ License: GPL-2+ Files: src/core/scoped_nsobject.h src/core/scoped_cftyperef.h + src/core/scoped_nsautorelease_pool.* Copyright: 2011, The Chromium Authors License: BSD-Google diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 69501556c..54a53f28e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -728,6 +728,7 @@ optional_source(APPLE core/macfslistener.mm core/macglobalshortcutbackend.mm core/mac_startup.mm + core/scoped_nsautorelease_pool.mm devices/macdevicelister.mm ui/globalshortcutgrabber.mm ui/macscreensaver.cpp diff --git a/src/core/mac_startup.mm b/src/core/mac_startup.mm index 25ca89ae7..7aa45d3dd 100644 --- a/src/core/mac_startup.mm +++ b/src/core/mac_startup.mm @@ -21,7 +21,6 @@ #import #import -#import #import #import #import @@ -47,6 +46,7 @@ #include "utilities.h" #include "core/logging.h" #include "core/scoped_cftyperef.h" +#include "core/scoped_nsautorelease_pool.h" #ifdef HAVE_SPARKLE #import @@ -85,7 +85,7 @@ static bool BreakpadCallback(int, int, mach_port_t, void*) { } static BreakpadRef InitBreakpad() { - NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + ScopedNSAutoreleasePool pool; BreakpadRef breakpad = nil; NSDictionary* plist = [[NSBundle mainBundle] infoDictionary]; if (plist) { @@ -236,7 +236,7 @@ static BreakpadRef InitBreakpad() { namespace mac { void MacMain() { - [[NSAutoreleasePool alloc] init]; + ScopedNSAutoreleasePool pool; // Creates and sets the magic global variable so QApplication will find it. [MacApplication sharedApplication]; #ifdef HAVE_SPARKLE @@ -273,8 +273,7 @@ QString GetResourcesPath() { } QString GetApplicationSupportPath() { - NSAutoreleasePool* pool = [NSAutoreleasePool alloc]; - [pool init]; + ScopedNSAutoreleasePool pool; NSArray* paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory, NSUserDomainMask, @@ -286,13 +285,11 @@ QString GetApplicationSupportPath() { } else { ret = "~/Library/Application Support"; } - [pool drain]; return ret; } QString GetMusicDirectory() { - NSAutoreleasePool* pool = [NSAutoreleasePool alloc]; - [pool init]; + ScopedNSAutoreleasePool pool; NSArray* paths = NSSearchPathForDirectoriesInDomains( NSMusicDirectory, NSUserDomainMask, @@ -304,7 +301,6 @@ QString GetMusicDirectory() { } else { ret = "~/Music"; } - [pool drain]; return ret; } diff --git a/src/core/scoped_nsautorelease_pool.h b/src/core/scoped_nsautorelease_pool.h new file mode 100644 index 000000000..fb8a79d56 --- /dev/null +++ b/src/core/scoped_nsautorelease_pool.h @@ -0,0 +1,38 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_ +#define BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_ + +#include + +#if defined(__OBJC__) +@class NSAutoreleasePool; +#else // __OBJC__ +class NSAutoreleasePool; +#endif // __OBJC__ + +// ScopedNSAutoreleasePool allocates an NSAutoreleasePool when instantiated and +// sends it a -drain message when destroyed. This allows an autorelease pool to +// be maintained in ordinary C++ code without bringing in any direct Objective-C +// dependency. + +class ScopedNSAutoreleasePool { + public: + ScopedNSAutoreleasePool(); + ~ScopedNSAutoreleasePool(); + + // Clear out the pool in case its position on the stack causes it to be + // alive for long periods of time (such as the entire length of the app). + // Only use then when you're certain the items currently in the pool are + // no longer needed. + void Recycle(); + private: + NSAutoreleasePool* autorelease_pool_; + + private: + Q_DISABLE_COPY(ScopedNSAutoreleasePool); +}; + +#endif // BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_ diff --git a/src/core/scoped_nsautorelease_pool.mm b/src/core/scoped_nsautorelease_pool.mm new file mode 100644 index 000000000..d37a7bea9 --- /dev/null +++ b/src/core/scoped_nsautorelease_pool.mm @@ -0,0 +1,24 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "scoped_nsautorelease_pool.h" + +#import + +ScopedNSAutoreleasePool::ScopedNSAutoreleasePool() + : autorelease_pool_([[NSAutoreleasePool alloc] init]) { + Q_ASSERT(autorelease_pool_); +} + +ScopedNSAutoreleasePool::~ScopedNSAutoreleasePool() { + [autorelease_pool_ drain]; +} + +// Cycle the internal pool, allowing everything there to get cleaned up and +// start anew. +void ScopedNSAutoreleasePool::Recycle() { + [autorelease_pool_ drain]; + autorelease_pool_ = [[NSAutoreleasePool alloc] init]; + Q_ASSERT(autorelease_pool_); +} diff --git a/src/devices/macdevicelister.mm b/src/devices/macdevicelister.mm index 02ecafb91..963549e3f 100644 --- a/src/devices/macdevicelister.mm +++ b/src/devices/macdevicelister.mm @@ -20,6 +20,7 @@ #include "mtpconnection.h" #include "core/logging.h" #include "core/scoped_cftyperef.h" +#include "core/scoped_nsautorelease_pool.h" #include "core/scoped_nsobject.h" #include @@ -31,7 +32,6 @@ #include #import -#import #import #import #import @@ -113,7 +113,7 @@ MacDeviceLister::~MacDeviceLister() { } void MacDeviceLister::Init() { - [[NSAutoreleasePool alloc] init]; + ScopedNSAutoreleasePool pool; // Populate MTP Device list. if (sMTPDeviceList.empty()) { diff --git a/src/widgets/osd_mac.mm b/src/widgets/osd_mac.mm index 008e36137..9c8ecdba3 100644 --- a/src/widgets/osd_mac.mm +++ b/src/widgets/osd_mac.mm @@ -24,6 +24,8 @@ #import +#include "core/scoped_nsautorelease_pool.h" + @interface GrowlInterface :NSObject { } -(void) SendGrowlAlert:(NSString*)message title:(NSString*)title image:(NSData*)image; @@ -120,6 +122,7 @@ class OSD::GrowlNotificationWrapper { private: GrowlInterface* growl_interface_; + ScopedNSAutoreleasePool pool_; }; void OSD::Init() {