bug#81610: 30.2; [PATCH] NS; NSRangeException when enabling tool-bar-mode

Yavor Doganov <[email protected]>
Newsgroups gmane.emacs.bugs
Organization The GNU Emacs Church (Bulgarian Eparchy)
Message-ID <87se4ezluz.GNU'[email protected]>
On Thu, 13 Aug 2026 23:20:00 +0300,
Alan Third wrote:
> On Wed, Aug 12, 2026 at 06:01:18PM +0300, Eli Zaretskii wrote:
> > > From: Yavor Doganov <[email protected]>
> > > Notice that in GDB frames 2 and 3 there are two distinct EmacsToolbar
> > > instances for a single Emacs frame.  The one from GDB frame 2,
> > > 0x555557a87500, is a "parasite" object: it is initialized at frame
> > > creation and toolbar items are never insterted in it.
> > 
> > Alan, any comments to the patch?  Because it's definitely
> > macOS-specific.
> 
> This doesn't look right to me,

Could you please explain why do you think so?

The comment at free_frame_tool_bar says that we're only hiding the
toolbar, so the same object that was initially and unconditionally
created in EmacsWindow -initWithEmacsFrame:fullscreen:screen:
(nsterm.m:9844) should be reused.  The following change demonstrates
what's wrong -- it discards the exception and prints the just
initialized EmacsToolbar object:

diff --git a/src/nsmenu.m b/src/nsmenu.m
index ab6e7d7140d..5b0b289a6e4 100644
--- a/src/nsmenu.m
+++ b/src/nsmenu.m
@@ -1277,6 +1277,7 @@ - (instancetype)initForView: (EmacsView *)view withIdentifier: (NSString *)ident
   activeIdentifiers = [[NSMutableArray alloc] initWithCapacity: 8];
   prevIdentifiers = nil;
   prevEnablement = enablement = 0L;
+  GSPrintf (stdout, @"%@, identifier: %@\n", self, identifier);
   return self;
 }
 
@@ -1307,7 +1308,10 @@ - (void) clearAll
 
   [self clearActive];
   while ([[self items] count] > 0)
+    NS_DURING
     [self removeItemAtIndex: 0];
+    NS_HANDLER
+    NS_ENDHANDLER
 }
 

With this change, if I run "emacs -Q" and type several times "M-x
tool-bar-mode RET", I get:

<EmacsToolbar: 0x55b37539c050>, identifier: 0x55b373d3d1d0
<EmacsToolbar: 0x55b3750914d0>, identifier: 0x55b373d3d1d0
<EmacsToolbar: 0x55b373f1b1b0>, identifier: 0x55b373d3d1d0

That's for a single Emacs frame.  With the patch I proposed, there's
only one object and there's no exception.

The additional object is created precisely because the toolbar is set
to nil in free_frame_tool_bar, then the "toolbar" local variable in
update_frame_tool_bar is nil (because -toolbar returns nil), which
leads to another call to -createToolbar:.

Since we're only hiding the toolbar, -setVisible: in
free_frame_tool_bar is sufficient and -setToolbar: does only harm
(memory leak + exception when you start Emacs with the toolbar
disabled).
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.