Subject: bug#32812: 27.0.50; macOS Mojave GNU Emacs crash after 5-10 minutes or so

From: Alan Third <[email-masked]>
Date: Mon, 24 Sep 2018 11:17:06 +0100

On Sun, Sep 23, 2018 at 04:21:13PM +0100, Zack Piper wrote:
> Hi!
> 
> (Replacing my post in #31904 with this since they're separate issues(?))

Sorry, I somehow missed that previous email.

> I've applied a slightly modified (i.e. updated) version of Alan's patch from
> bug #31904
> (attached) to fix an issue concerning the buffer and mode-line not being
> rendered. The patch works great, everything is now rendered.
> 
> Now I notice that after 5-10 minutes (or even before), Emacs crashes with
> the below:
> 
> ```
> objc[82784]: Invalid or prematurely-freed autorelease pool 0x1030032e0.
> ```
> 
> This started occurring since upgrading to macOS Mojave, so possibly there's
> a bug with Mojave or just some incompatibility, not really sure!

It’s a bit strange looking, and autorelease pools are something of a
mystery to me.

I have a suspicion, though, that just creating a new autorelease pool
may solve this. I’ve attached a patch. It’s for emacs-26, but should
apply OK to master, or without too much work anyway.

It simply wraps the call to displayIfNeeded in a new pool:

static void
ns_flush_display (struct frame *f)
/* Force the frame to redisplay.  If areas have previously been marked
   dirty by setNeedsDisplayInRect (in ns_focus), then this will call
   draw_rect: which will "expose" those areas.  */
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  [FRAME_NS_VIEW (f) displayIfNeeded];
  [pool release];
}

Thanks for testing the patch and raising this issue. There’s not been
a lot of feedback so far and I think we’re going to have to commit the
patch soon given that Mojave is out today.
-- 
Alan Third
>From 17bab071cc15aa378e96913ea84167fc4af88f02 Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Mon, 24 Sep 2018 11:02:41 +0100
Subject: [PATCH] Fix crash when flushing to screen (bug#32812)

* src/nsterm.m (ns_flush_display): Create a new autorelease pool for
the display code.
---
 src/nsterm.m | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/nsterm.m b/src/nsterm.m
index b36d847eb3..d6f6d8afee 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1252,7 +1252,9 @@ static NSRect constrain_frame_rect(NSRect frameRect, bool isFullscreen)
    dirty by setNeedsDisplayInRect (in ns_focus), then this will call
    draw_rect: which will "expose" those areas.  */
 {
+  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   [FRAME_NS_VIEW (f) displayIfNeeded];
+  [pool release];
 }

-- 
2.18.0