Important: Please read the Qt Code of Conduct -

Crash on launch with file on Mac

  • I'm developing a desktop application, and I'm working on getting it to launch with a file from Finder on Mac OS X. I've subclassed the application object and overridden event() to handle the QFileOpenEvent, but every time I launch the app by double-clicking a file, it immediately crashes. This happens even if I don't override event() at all, so it's not a bug in my event handler. Of course, I can't do this in the debugger, so I can't tell exactly where it's crashing, but the stack shown in Apple's crash report looks like this (note that there are no stack frames within my application code):

    @Thread 0 Crashed: Dispatch queue:
    0 QtCore 0x0000000100d671da QString::fromLocal8Bit(char const*, int) + 42
    1 QtCore 0x0000000100e72dac QCoreApplication::arguments() + 124
    2 QtGui 0x00000001000de8ef -[QCocoaApplicationDelegate application:openFiles:] + 223
    3 0x00007fff805f3b52 -[NSApplication(NSAppleEventHandling) _handleAEOpenDocumentsForURLs:] + 505
    4 0x00007fff804c0065 -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 217
    5 0x00007fff800f90d6 -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 360
    6 0x00007fff800f8f06 _NSAppleEventManagerGenericHandler + 114
    7 0x00007fff82b1c32b aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned int, unsigned char*) + 162
    8 0x00007fff82b1c224 dispatchEventAndSendReply(AEDesc const*, AEDesc*) + 32
    9 0x00007fff82b1c12b aeProcessAppleEvent + 210
    10 0x00007fff875ed619 AEProcessAppleEvent + 48
    11 0x00007fff803c5095 _DPSNextEvent + 1191
    12 0x00007fff803c4801 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 155
    13 0x00007fff8038a68f -[NSApplication run] + 395
    14 QtGui 0x00000001000e7c04 QEventDispatcherMac::processEvents(QFlagsQEventLoop::ProcessEventsFlag) + 1588
    15 QtCore 0x0000000100e70774 QEventLoop::processEvents(QFlagsQEventLoop::ProcessEventsFlag) + 68
    16 QtCore 0x0000000100e70a94 QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) + 180
    17 QtCore 0x0000000100e720bc QCoreApplication::exec() + 188
    18 com.blastedrock.MapMaker 0x0000000100003447 main + 46 (main.cpp:31)
    19 com.blastedrock.MapMaker 0x00000001000032e0 start + 52
    Anybody have any idea what's going on? I've looked at the "browser" sample, which handles this event, and I don't see anything that they're doing differently than what I'm doing.


  • OK, let's ask this question another way, then. Has anybody used the FileOpenEvent on the Mac? Can anyone provide any sample code that's known to work?


  • I never used that feature myself. You might want to have a look at the demos/browser demo in the Qt sources. It uses QFileOpenEvent to open HTML pages in that demo browser.

  • Appreciate the response. I did look at the browser sample, and it looks to me like they're doing the same thing that I'm doing. I may need to fall back to the old approach of incrementally changing their code into my code, but it's so different that I was hoping to avoid that.


  • Oh. Just looking at your stack trace: Can you provide the exact declaration and implementation of you constructors of your derived QApplication subclass, please? I have a guess what might be wrong here.

  • Here's what I've got at the moment. Note that the crash still happens without any of the code in the body of either function.

    explicit MapMakerApplication(int argc, char **argv);

    MapMakerApplication::MapMakerApplication(int argc, char **argv)
    : QApplication(argc, argv)
    logFile = fopen("/Users/asher/logs/MMLog.txt", "a");
    fprintf(logFile, "------------------------------------------------------------------\nStarting log\n");
    for (int i = 0; i < _windows.size(); ++i)

    If it helps, I'm never actually getting the FileOpenEvent before the crash. However, if I start the application directly or through the debugger, it launches fine.


  • Just to be clear, though, if I don't subclass QApplication, I have no trouble, so it's something about the structure of this class.


  • Ok, exactly what I have guessed. Your problem has nothing to do with the QFileOpenEvent. Just change your constructor signature


    explicit MapMakerApplication(int argc, char **argv);


    explicit MapMakerApplication(int &argc, char **argv);

    Note the reference ampersand for argc! There are some threads around in the forums that explain the faulty behavior. Basically it's due to QCoreApplication's requirement of argc to stay valid during its lifetime, which is not the case for pass-by-value arguments.


    [quote author="aaronsher" date="1342628763"]Just to be clear, though, if I don't subclass QApplication, I have no trouble, so it's something about the structure of this class.

    Which proofs my guess even more ;-)

  • Cool, thanks! Wow, that's one I never would have guessed, though looking again at the browser sample I see that they are doing that and I missed it the first time.


  • You're welcome. And you're not the first who stepped into that trap :-)

    I've added doc notes the QCoreApplication and QApplication documentation here on DevNet.

  • Hello,
    I have a similar problem but I can't find this "browser" demo. Can someone tell me which folder is it in?


  • It's under the QtSDK in Demos/4.7/browser in my install.


  • Aah so this demo is QT 4.7 specific! I am using Qt 4.8 and this demo wasn't available. I just downloaded the 4.7 version to get the demo. Thank you.

  • [quote author="aaronsher" date="1342629125"]Cool, thanks! Wow, that's one I never would have guessed, though looking again at the browser sample I see that they are doing that and I missed it the first time.


    Please mark the title as Solved :)

  • [SOLVED] How do I do that?

    1. Click on the title of your post
      you will find this below the left side of your title.

    quote |link |edit |report

    1. click on edit
    2. Prefix the title by adding a tag like [SOLVED] and update the post.

    Thank you.

  • bq. [SOLVED] How do I do that?

    looks great mickael.cruz

  • Hi All,
    I am getting very similar crash using Qt 4.8.5 on OSX 10.9

    When I double click my file in Finder while my application is running it opens correctly, but if my application is not already running I get a crash just after showing its splash screen, note that I am using the correct int& argc in my inherited QApplication

    Part of the crash stack:

    Thread 0 Crashed:: Dispatch queue:
    0 QtCore 0x000000010315614a QString::fromLocal8Bit(char const*, int) + 42
    1 QtCore 0x000000010321d9cc QCoreApplication::arguments() + 124
    2 QtGui 0x000000010356a77f QMacInputContext::reset() + 49311
    3 0x00007fff8810ef65 __69-[NSApplication(NSAppleEventHandling) _handleAEOpenDocumentsForURLs:]_block_invoke + 1651
    4 0x00007fff87f0ac63 __78-[NSDocumentController(NSInternal) _autoreopenDocumentsWithCompletionHandler:]_block_invoke_2 + 140
    5 0x00007fff87f0a84d -[NSDocumentController(NSInternal) _autoreopenDocumentsWithCompletionHandler:] + 746
    6 0x00007fff87d759fb -[NSApplication _reopenWindowsAsNecessaryIncludingRestorableState:registeringAsReady:completionHandler:] + 323
    7 0x00007fff8810e869 -[NSApplication(NSAppleEventHandling) _handleAEOpenDocumentsForURLs:] + 306
    8 0x00007fff87d752bb -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 450
    9 0x00007fff89a0eeaa -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 294
    10 0x00007fff89a0ed1d _NSAppleEventManagerGenericHandler + 106
    11 0x00007fff83d55e1f aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned int, unsigned char*) + 381
    12 0x00007fff83d55c32 dispatchEventAndSendReply(AEDesc const*, AEDesc*) + 31
    13 0x00007fff83d55b36 aeProcessAppleEvent + 315
    14 0x00007fff877495f1 AEProcessAppleEvent + 56
    15 0x00007fff87d710f6 _DPSNextEvent + 1026
    16 0x00007fff87d708db -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122
    17 0x00007fff87d649cc -[NSApplication run] + 553
    18 QtGui 0x0000000103572171 QDesktopWidget::resizeEvent(QResizeEvent*) + 3553
    19 QtGui 0x0000000103574070 QDesktopWidget::resizeEvent(QResizeEvent*) + 11488
    20 QtCore 0x000000010321c745 QCoreApplication::processEvents(QFlagsQEventLoop::ProcessEventsFlag) + 69
    21 QtGui 0x0000000103614aaf QWidgetPrivate::show_helper() + 639
    22 QtGui 0x0000000103614cff QWidget::setVisible(bool) + 511

    Last thing, I found when outputting application argc in exec function that it is not 1 as when I run it from QCreator, it is 24576!

    I hope someone had found a solution for this because I've been trying to sort it for a week

    thanks in advance

  • Please ignore the above comment as it appeared to be the same issue, and the previous fix sorted it

Log in to reply