Use @autoreleasepool to avoid accumulating objects

This commit is contained in:
David Capello 2017-01-10 12:24:22 -03:00
parent 0b37d23c37
commit 5ba071e760
2 changed files with 46 additions and 42 deletions

View File

@ -1,5 +1,5 @@
// SHE library
// Copyright (C) 2015-2016 David Capello
// Copyright (C) 2015-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -19,40 +19,42 @@ void OSXEventQueue::getEvent(Event& ev, bool canWait)
{
ev.setType(Event::None);
retry:;
NSApplication* app = [NSApplication sharedApplication];
if (!app)
return;
@autoreleasepool {
retry:;
NSApplication* app = [NSApplication sharedApplication];
if (!app)
return;
// Pump the whole queue of Cocoa events
NSEvent* event;
do {
event = [app nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event) {
// Intercept <Control+Tab>, <Cmd+[>, and other keyboard
// combinations, and send them directly to the main
// NSView. Without this, the NSApplication intercepts the key
// combination and use it to go to the next key view.
if (event.type == NSKeyDown) {
[app.mainWindow.contentView keyDown:event];
// Pump the whole queue of Cocoa events
NSEvent* event;
do {
event = [app nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event) {
// Intercept <Control+Tab>, <Cmd+[>, and other keyboard
// combinations, and send them directly to the main
// NSView. Without this, the NSApplication intercepts the key
// combination and use it to go to the next key view.
if (event.type == NSKeyDown) {
[app.mainWindow.contentView keyDown:event];
}
else {
[app sendEvent:event];
}
}
else {
[app sendEvent:event];
}
}
} while (event);
} while (event);
if (!m_events.try_pop(ev)) {
if (canWait) {
// Wait until there is a Cocoa event in queue
[NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:NO];
goto retry;
if (!m_events.try_pop(ev)) {
if (canWait) {
// Wait until there is a Cocoa event in queue
[NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:NO];
goto retry;
}
}
}
}

View File

@ -1,5 +1,5 @@
// SHE library
// Copyright (C) 2012-2016 David Capello
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -113,14 +113,16 @@ public:
}
void updateWindow(const gfx::Rect& bounds) {
int scale = this->scale();
NSView* view = m_window.contentView;
[view setNeedsDisplayInRect:
NSMakeRect(bounds.x*scale,
view.frame.size.height - (bounds.y+bounds.h)*scale,
bounds.w*scale,
bounds.h*scale)];
[view displayIfNeeded];
@autoreleasepool {
int scale = this->scale();
NSView* view = m_window.contentView;
[view setNeedsDisplayInRect:
NSMakeRect(bounds.x*scale,
view.frame.size.height - (bounds.y+bounds.h)*scale,
bounds.w*scale,
bounds.h*scale)];
[view displayIfNeeded];
}
}
void setTranslateDeadKeys(bool state) {
@ -304,7 +306,7 @@ private:
return;
bitmap.lockPixels();
{
@autoreleasepool {
NSGraphicsContext* gc = [NSGraphicsContext currentContext];
CGContextRef cg = (CGContextRef)[gc graphicsPort];
CGColorSpaceRef colorSpace = CGDisplayCopyColorSpace(CGMainDisplayID());