Tk Source Code

View Ticket
Login
2026-10-04
11:11 • Closed ticket [d39d1f86]: Tcl panic on macOS after <tell application "Wish" to open> plus 5 other changes artifact: f7bc8c3e user: serhiy.storchaka
2021-11-26
19:51
Fix [794100ec81]: crashes when handling Apple Events. check-in: be960311 user: culler tags: trunk, main
19:49 • Closed ticket [794100ec]: macOS Aqua : Tk crash when tk::mac::OpenDocument is not defined plus 10 other changes artifact: 5700cc1f user: marc_culler
19:33 • Ticket [794100ec]: 3 changes artifact: fe1f2a2e user: nab
16:43 • Ticket [794100ec]: 4 changes artifact: 419936c1 user: marc_culler
16:09 • Ticket [794100ec]: 4 changes artifact: 90525008 user: marc_culler
09:12 • Ticket [794100ec]: 3 changes artifact: 8888b3b3 user: nab
03:24 • Ticket [794100ec]: 4 changes artifact: 634f30cd user: marc_culler
01:34 • Ticket [794100ec]: 4 changes artifact: efb0a1e4 user: marc_culler
00:43 • Ticket [794100ec]: 4 changes artifact: 75f605e6 user: marc_culler
00:38 • Ticket [794100ec]: 4 changes artifact: 7bb3e7a9 user: marc_culler
2021-11-07
21:54 • Ticket [794100ec]: 3 changes artifact: f9cef84a user: kevin_walzer
07:49 • Ticket [794100ec]: 3 changes artifact: 703cf287 user: nab
2021-11-06
20:39 • Ticket [794100ec]: 3 changes artifact: a1cc4b75 user: kevin_walzer
17:54 • New ticket [794100ec]. artifact: 81c456c7 user: nab

Ticket UUID: 794100ec810049b6a561762ada57455551540afe
Title: macOS Aqua : Tk crash when tk::mac::OpenDocument is not defined
Type: Bug Version: trunk
Submitter: nab Created on: 2021-11-06 17:54:04
Subsystem: 66. Aqua Window Operations Assigned To: marc_culler
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2021-11-26 19:49:11
Resolution: Fixed Closed By: marc_culler
    Closed on: 2021-11-26 19:49:11
Description:
Hi Marc,
I recently read discussion on MacTcl mailing list between Jasper and Kevin about opening app by double-clicking or dragging files.
I didn't knew about tk::mac::OpenDocument so I didn't define this proc in my app.
it results a crash like that (surely a NULL ptr passed to strncmp).

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libsystem_platform.dylib      	    0x7ff811cc95e0 _platform_strncmp + 320
1   Tcl                           	       0x10638e674 Tcl_FindCommand + 68 (tclNamesp.c:2593)
2   Tk                            	       0x106777596 ProcessAppleEvent + 54 (tkMacOSXHLEvents.c:451)
3   Tcl                           	       0x1063b5ba1 TimerHandlerEventProc + 113 (tclTimer.c:593)
4   Tcl                           	       0x106391cdc Tcl_ServiceEvent + 156 (tclNotify.c:704)
5   Tcl                           	       0x106391fa8 Tcl_DoOneEvent + 360 (tclNotify.c:1001)
6   Tk                            	       0x1066b7b9b Tk_MainLoop + 43 (tkEvent.c:2124)
7   Tk                            	       0x1066c5cf2 Tk_MainEx + 1218 (tkMain.c:377)
8   DLight                        	       0x105d97bab main + 139 (main.c:780)
9   dyld                          	       0x10a9e84fe start + 462

++
Nicolas
User Comments: marc_culler (claiming to be Marc Culler) added on 2021-11-26 19:49:11:
Thanks!

I think your app *could* process the files if you defined the openDocument
function, however.

I will merge the bugfix brabch into main and close the ticket.

nab added on 2021-11-26 19:33:27:
yes, with the bug fix branch, Wish no longer crash, my app neither.

Wish does not run the double-clicked script (same as you), and my app does not process its double-clicked file

++

marc_culler (claiming to be Marc Culler) added on 2021-11-26 16:43:11:
To be specific, all of the calls to:
   Tcl_DoWhenIdle(ProcessAppleEvent, (ClientData)AEInfo);
should have been removed in the merge.  Leaving them in place creates
a race between the idle tasks and the timer tasks for who will free the
AEInfo, and the loser crashes.

I have removed those lines in the bugfix branch.  I no longer see a
crash when double-clicking a .tcl file.  (Wish starts but ignores the
file.)

Nicolas, can you please test?  Thanks.

marc_culler (claiming to be Marc Culler) added on 2021-11-26 16:09:09:
I think I see what is going on here.  There is no problem with core-8-6-branch
but tkMacOSXHLEvents.c is full of weird stuff in the main branch.

With the core-8-6-branch double-clicking a .tcl file does not cause a crash.
Wish opens and immediately exits.  I am not sure what the point of that is
supposed to be, but at least there is no crash.

Incidentally, I also checked that IDLE handles dropped and clicked files
correctly with 8.6.12 in place of the 8.6.11 that ships with Python 3.10.

So I guess this is just a matter of screwing up a merge.

nab added on 2021-11-26 09:12:02:
Hi Marc,
thank you for taking time on this.
I do not do Perl (it's Christopher that is doing Perl), my app is Tcl+C.
I do my tests on macOS12, and like you it starts messing on tkMacOSXHLEvents.c:451, but my ticket seems related to ::tk::mac::OpenDocument not defined in my app and so passing a NULL ptr to Tcl_FindCommand and thus to strncmp.

I have tried to double-click on a .tcl script and I have the same crash as you.

my version of Tcl is core-8-branch

++

marc_culler (claiming to be Marc Culler) added on 2021-11-26 03:24:53:
The code in tkMacOSXHLEvents.c looks dangerous to me.  The
function ProcessAppleEvent frees the AEInfo that is passed to
it as clientData if the function runs to the end. But there
are several places where first a call to ProcessAppleEvent
is registered as an idle task and, immediately after that,
ProcessAppleEvent is called directly.  If the direct call
succeeds then the idle task will be run with clientData that has
already been freed.  To make it even more complicated, if the Tcl
command is not found then ProcessAppleEvent schedules itself as
a timer task to run in 200 milliseconds, and it might do that twice.
If both attempts fail it will free the AEInfo.  So potentially
we have ProcessAppleEvent scheduled to run as an idle task and as
a timer task, both of which will run after the direct call.  If
one of those calls succeeds and frees the AEInfo then the later
calls will probably crash.  There is also another place where
ProcessAppleEvent is scheduled as a timer task to run in 500 ms.

With this scheme the chances of either a double free or corrupt
data are extremely high.

marc_culler (claiming to be Marc Culler) added on 2021-11-26 01:34:59:
One more data point.  On my Monterey laptop, double-clicking on a .tcl
file causes a crash in Tk. It is a SIG_ABORT crash, not SIG_SEGV, and
it does not look like the one in this ticket.  It crashes in TclpFree:

0   libsystem_kernel.dylib        	    0x7ff809fce112 __pthread_kill + 10
1   libsystem_pthread.dylib       	    0x7ff80a004233 pthread_kill + 263
2   libsystem_c.dylib             	    0x7ff809f50d10 abort + 123
3   Tcl                           	       0x107cebd77 Tcl_PanicVA + 333
4   Tcl                           	       0x107cebe0a Tcl_Panic + 111
5   Tcl                           	       0x107d03ee0 TclpFree + 226
6   Tk                            	       0x107b0c809 ProcessAppleEvent + 201 (tkMacOSXHLEvents.c:456)
7   Tcl                           	       0x107d05756 0x107c2a000 + 898902
8   Tcl                           	       0x107ce87cf Tcl_ServiceEvent + 136
9   Tcl                           	       0x107ce8a77 Tcl_DoOneEvent + 332
10  Tk                            	       0x1079c849d Tk_MainLoop + 45 (tkEvent.c:2124)
11  Tk                            	       0x1079e095a Tk_MainEx + 2394 (tkMain.c:377)
12  Wish                          	       0x107608e10 0x107605000 + 15888
13  dyld                          	       0x113e7a4fe start + 462

marc_culler (claiming to be Marc Culler) added on 2021-11-26 00:43:19:
Just to clarify, Wish does open when you drop a .tcl file on the icon,
but it does not run the script.

marc_culler (claiming to be Marc Culler) added on 2021-11-26 00:38:46:
I am confused by this ticket.  First, I thought that Nicolas' app was
written in Tcl, and I don't see any reference to Perl in the report.
Second, I don't know what Nicolas did to create the crash.  Drop a file
on the app icon, perhaps?

I also notice that Wish.app does not react when a tcl file is dropped
on the app icon.  Is that intentional?

kevin_walzer added on 2021-11-07 21:54:54:
It shouldn't crash - I wonder if something else is going on, as I don't have a clear idea of what is happening in your app when it crashes. 

You mentioned your app is written in Perl? Here's how I implement this command in a Perl app:

<verbatim>

if ($IS_AQUA) {
    Tkx::interp_alias( "", "::tk::mac::OpenDocument", "", [ \&openDoc ] );
}

#"open"event from file or directory dragged onto icon
sub openDoc {

    my $fname = shift;
    &selectFileSystem($fname);
}

#select directory from operations--this runs when drag and drop or from external script
sub selectFileSystem {

    my $fname = shift;
    my @name  = Tkx::SplitList($fname);
    my $path  = join "", @name;
    $folder = $path;

    if ( -f $folder ) {
        $listbox->delete( 0, "end" );
        @filelist = ();
        push( @filelist, $folder );
        foreach (@filelist) {
            $listbox->insert( "end", "$_" );
        }
    }

    if ( -d $folder ) {

        $listbox->delete( 0, "end" );
        @filelist = <"$folder/*">;
        foreach (@filelist) {
            $listbox->insert( "end", "$_" );
        }

        return $folder;
    }

    return $folder;
}
</verbatim>

nab added on 2021-11-07 07:49:19:
Hi Kevin,
thank you for your answer.
I'm traveling for a week so I'll try the week after.

if you remove the command, does it lead to a same crash?

++

kevin_walzer added on 2021-11-06 20:39:17:
The lack of definition of this command shouldn't cause any issue unless you are trying to associate your app with a file type that fires a command when a file of that type is double-clicked or is dropped onto your app icon. If so, can you try implementing the command?