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: com.apple.main-thread
    0 QtCore 0x0000000100d671da QString::fromLocal8Bit(char const*, int) + 42
    1 QtCore 0x0000000100e72dac QCoreApplication::arguments() + 124
    2 QtGui 0x00000001000de8ef -[QCocoaApplicationDelegate application:openFiles:] + 223
    3 com.apple.AppKit 0x00007fff805f3b52 -[NSApplication(NSAppleEventHandling) _handleAEOpenDocumentsForURLs:] + 505
    4 com.apple.AppKit 0x00007fff804c0065 -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 217
    5 com.apple.Foundation 0x00007fff800f90d6 -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 360
    6 com.apple.Foundation 0x00007fff800f8f06 _NSAppleEventManagerGenericHandler + 114
    7 com.apple.AE 0x00007fff82b1c32b aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned int, unsigned char*) + 162
    8 com.apple.AE 0x00007fff82b1c224 dispatchEventAndSendReply(AEDesc const*, AEDesc*) + 32
    9 com.apple.AE 0x00007fff82b1c12b aeProcessAppleEvent + 210
    10 com.apple.HIToolbox 0x00007fff875ed619 AEProcessAppleEvent + 48
    11 com.apple.AppKit 0x00007fff803c5095 _DPSNextEvent + 1191
    12 com.apple.AppKit 0x00007fff803c4801 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 155
    13 com.apple.AppKit 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.

    Thanks,
    Aaron



  • 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?

    Thanks,
    Aaron



  • 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.

    Thanks,
    Aaron



  • 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::MapMakerApplication(int argc, char **argv)
    : QApplication(argc, argv)
    {
    logFile = fopen("/Users/asher/logs/MMLog.txt", "a");
    fprintf(logFile, "------------------------------------------------------------------\nStarting log\n");
    fflush(logFile);
    }
    MapMakerApplication::~MapMakerApplication()
    {
    for (int i = 0; i < _windows.size(); ++i)
    delete _windows.at(i);
    _windows.clear();
    }@

    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.

    Thanks,
    Aaron



  • 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.

    Aaron



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

    from

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

    to

    @
    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.

    PS:

    [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.
    [/quote]

    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.

    Aaron



  • 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?

    Thanks



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

    Aaron



  • 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.

    Aaron[/quote]

    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: com.apple.main-thread
    0 QtCore 0x000000010315614a QString::fromLocal8Bit(char const*, int) + 42
    1 QtCore 0x000000010321d9cc QCoreApplication::arguments() + 124
    2 QtGui 0x000000010356a77f QMacInputContext::reset() + 49311
    3 com.apple.AppKit 0x00007fff8810ef65 __69-[NSApplication(NSAppleEventHandling) _handleAEOpenDocumentsForURLs:]_block_invoke + 1651
    4 com.apple.AppKit 0x00007fff87f0ac63 __78-[NSDocumentController(NSInternal) _autoreopenDocumentsWithCompletionHandler:]_block_invoke_2 + 140
    5 com.apple.AppKit 0x00007fff87f0a84d -[NSDocumentController(NSInternal) _autoreopenDocumentsWithCompletionHandler:] + 746
    6 com.apple.AppKit 0x00007fff87d759fb -[NSApplication _reopenWindowsAsNecessaryIncludingRestorableState:registeringAsReady:completionHandler:] + 323
    7 com.apple.AppKit 0x00007fff8810e869 -[NSApplication(NSAppleEventHandling) _handleAEOpenDocumentsForURLs:] + 306
    8 com.apple.AppKit 0x00007fff87d752bb -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 450
    9 com.apple.Foundation 0x00007fff89a0eeaa -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 294
    10 com.apple.Foundation 0x00007fff89a0ed1d _NSAppleEventManagerGenericHandler + 106
    11 com.apple.AE 0x00007fff83d55e1f aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned int, unsigned char*) + 381
    12 com.apple.AE 0x00007fff83d55c32 dispatchEventAndSendReply(AEDesc const*, AEDesc*) + 31
    13 com.apple.AE 0x00007fff83d55b36 aeProcessAppleEvent + 315
    14 com.apple.HIToolbox 0x00007fff877495f1 AEProcessAppleEvent + 56
    15 com.apple.AppKit 0x00007fff87d710f6 _DPSNextEvent + 1026
    16 com.apple.AppKit 0x00007fff87d708db -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122
    17 com.apple.AppKit 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.