简体   繁体   中英

Cocoa Exception in Debug mode only

I'm getting a strange exception when using Cocoa together with a personal C++ library, while the exception only occurs in debug mode - in release mode everything is fine. And to stress that, it suffices to link my C++ library to get the exception, I don't need to call my library at all.

The error I get is "Exception: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)".

The C++ library is built using CMake and the mode is determined by the internal "CMAKE_BUILD_TYPE". The library can be found on GitHub, though I don't think that anyone wants to build it. Also because it is quite huge, I don't think that someone wants to look at the code.

A very minimal example to demonstrate the error is the following one:

#include <Cocoa/Cocoa.h>

int main()
{
    [NSApplication sharedApplication];
    printf("success\n");
    return 0;
}

As you can see, this example does not use my lib at all.

  • Compiling it with "clang main.mm -framework Cocoa" works just fine
  • Compiling it with "clang main.mm -framework Cocoa -lmylib" works also. "mylib" is built here with "-DCMAKE_BUILD_TYPE=Release"
  • Compiling it with "clang main.mm -framework Cocoa -lmylib_d" yields the above exception. "lmylib_d" is built here with "DCMAKE_BUILD_TYPE=Debug". It prints: "Segmentation fault: 11".

I don't understand it. What could I possible have done in my library that crashes Cocoa just because I link to it? The only thing that could come to my mind is that I have defined my own global operator new and delete. Still, Cocoa wouldn't use those, right?

Here is the full stack trace:

(anonymous namespace)::get_registry() (.8898) 0x00007fff564afd79
SLSNewConnection 0x00007fff56473dc4
SLSMainConnectionID 0x00007fff56474a87
_CFAppSleepSetupCoreGraphics 0x00007fff34649091
____CFRunLoopSetOptionsReason_block_invoke_2 0x00007fff34648738
_dispatch_client_callout 0x00007fff5c43ddb8
dispatch_once_f 0x00007fff5c43dd6b
__CFRunLoopSetOptionsReason 0x00007fff34646ff3
_LSApplicationCheckIn 0x00007fff35ae7abb
_RegisterApplication 0x00007fff32ec192c
GetCurrentProcess 0x00007fff32ec064c
MenuBarInstance::GetAggregateUIMode(unsigned int*, unsigned int*) 0x00007fff3391e4ab
MenuBarInstance::IsVisible() 0x00007fff3391e435
_NSInitializeAppContext 0x00007fff31bc1197
-[NSApplication init] 0x00007fff31bc0590
+[NSApplication sharedApplication] 0x00007fff31bc01e6
main main.mm:5
start 0x00007fff5c477015
start 0x00007fff5c477015

Thanks for any help in advance!

Does your library have any static initializers? Or, link to other libs that do? Those are run before main is called, and if they could be causing issues. Especially if they try to interact with Foundation/AppKit.

Regardless, I'm fairly sure it is not safe to use NSApplication this way. I believe you must access it within the context of a main thread which calls NSApplicationMain to setup the underlying AppKit machinery. Since you are crashing within that machinery, I think that is the underlying issue.

Indeed @Kai Guther was correct. Cocoa was using my overridden global new. Apparently I had a bug in my replaced new operator. After a bit of refactoring of that function, it works now. Thanks for the important information... I think without that guess I would've searched for ages for that terrible bug...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM