Xcode 5控制台应用程序仅在通过finder启动时才会出现段错误

时间:2014-08-21 08:05:47

标签: c++ xcode macos segmentation-fault osx-mavericks

我在Xcode中用c ++编写了一个控制台应用程序。当通过finder启动时(通过双击build目录中的可执行文件),我得到一个终端窗口,上面写着“Segmentation fault:11”。

但是,当我打开终端时,导航到该文件夹​​并通过执行./“可执行文件名”运行可执行文件“程序运行正常。在终端中的lldb实例中启动时也可以正常运行。

为什么会这样?

从控制台到应用程序的崩溃日志。下面:

Process:         UDPServer [3640]
Path:            /Users/USER/Desktop/*/UDPServer
Identifier:      UDPServer
Version:         0
Code Type:       X86-64 (Native)
Parent Process:  bash [3637]
Responsible:     Terminal [3047]
User ID:         501

Date/Time:       2014-08-21 09:38:55.153 +0100
OS Version:      Mac OS X 10.9.4 (13E28)
Report Version:  11
Anonymous UUID:  50B9AAFB-D335-BAF0-BE59-B5EDE60DC6A3

Sleep/Wake UUID: 5133BBF8-BBC6-4207-A12E-040C0E38E283

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000000

VM Regions Near 0:
--> 
    __TEXT                 0000000101b6a000-0000000101b74000 [   40K] r-x/rwx SM=COW  /Users/USER/Desktop/*

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   UDPServer                       0x0000000101b6f70e main + 230 (main.cpp:34)
1   libdyld.dylib                   0x00007fff85f245fd start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x00007fca0b500130  rbx: 0x00007fff74d5f2f8  rcx: 0x2e3836312e323931  rdx: 0x0000000000000000
  rdi: 0x00007fca0b50014e  rsi: 0x00007fff5e095b2e  rbp: 0x00007fff5e095b90  rsp: 0x00007fff5e0956b0
   r8: 0x000000000000000d   r9: 0x00007fca0b500000  r10: 0x000000000000000d  r11: 0xffffffcaad46a620
  r12: 0x00007fff74f79420  r13: 0x0000000000000000  r14: 0x0000000000000000  r15: 0x0000000000000000
  rip: 0x0000000101b6f70e  rfl: 0x0000000000010206  cr2: 0x0000000000000000

Logical CPU:     2
Error Code:      0x00000004
Trap Number:     14


Binary Images:
       0x101b6a000 -        0x101b73fff +UDPServer (0) <15B3E381-DAAC-34FC-A83D-5A6206126BE8> /Users/USER/Desktop/*/UDPServer
       0x101b89000 -        0x101b91ff7 +liboscpack.so (0) <1C20B5CF-B522-3D36-8B2D-B324AD4FCA62> /Users/USER/Desktop/*/liboscpack.so
       0x101ba0000 -        0x101c31fff +libboost_regex.dylib (0) <1A50E6A0-A616-3091-A6D1-4B4E8590A27F> /Users/USER/Desktop/*/libboost_regex.dylib
       0x101cbb000 -        0x101cbdff3 +libboost_system.dylib (0) <D55FA09E-42A9-3C49-836F-EF08DD5B42DF> /Users/USER/Desktop/*/libboost_system.dylib
    0x7fff6c0c2000 -     0x7fff6c0f5817  dyld (239.4) <042C4CED-6FB2-3B1C-948B-CAF2EE3B9F7A> /usr/lib/dyld
    0x7fff84bf1000 -     0x7fff84bf6ff7  libunwind.dylib (35.3) <78DCC358-2FC1-302E-B395-0155B47CB547> /usr/lib/system/libunwind.dylib
    0x7fff84d0b000 -     0x7fff84d0cff7  libsystem_blocks.dylib (63) <FB856CD1-2AEA-3907-8E9B-1E54B6827F82> /usr/lib/system/libsystem_blocks.dylib
    0x7fff84fbe000 -     0x7fff84fc4ff7  libsystem_platform.dylib (24.90.1) <3C3D3DA8-32B9-3243-98EC-D89B9A1670B3> /usr/lib/system/libsystem_platform.dylib
    0x7fff851e9000 -     0x7fff85210ffb  libsystem_info.dylib (449.1.3) <7D41A156-D285-3849-A2C3-C04ADE797D98> /usr/lib/system/libsystem_info.dylib
    0x7fff85613000 -     0x7fff85665fff  libc++.1.dylib (120) <4F68DFC5-2077-39A8-A449-CAC5FDEE7BDE> /usr/lib/libc++.1.dylib
    0x7fff8575e000 -     0x7fff8590bf27  libobjc.A.dylib (551.1) <AD7FD984-271E-30F4-A361-6B20319EC73B> /usr/lib/libobjc.A.dylib
    0x7fff859cf000 -     0x7fff859d0ff7  libSystem.B.dylib (1197.1.1) <E6BCC031-4342-318C-A1AD-A8E9AEAE716F> /usr/lib/libSystem.B.dylib
    0x7fff85c2e000 -     0x7fff85c2ffff  libunc.dylib (28) <62682455-1862-36FE-8A04-7A6B91256438> /usr/lib/system/libunc.dylib
    0x7fff85c58000 -     0x7fff85c5fffb  libcopyfile.dylib (103.92.1) <CF29DFF6-0589-3590-834C-82E2316612E8> /usr/lib/system/libcopyfile.dylib
    0x7fff85c96000 -     0x7fff85c97ff7  libDiagnosticMessagesClient.dylib (100) <4CDB0F7B-C0AF-3424-BC39-495696F0DB1E> /usr/lib/libDiagnosticMessagesClient.dylib
    0x7fff85d60000 -     0x7fff85d7bff7  libsystem_malloc.dylib (23.10.1) <A695B4E4-38E9-332E-A772-29D31E3F1385> /usr/lib/system/libsystem_malloc.dylib
    0x7fff85f21000 -     0x7fff85f24ff7  libdyld.dylib (239.4) <A1273A02-4928-3BAC-A846-6C069DC1A0A6> /usr/lib/system/libdyld.dylib
    0x7fff8655d000 -     0x7fff86561ff7  libsystem_stats.dylib (93.90.3) <337946FC-B2E1-3DFA-A8B5-30DA8D584D75> /usr/lib/system/libsystem_stats.dylib
    0x7fff86562000 -     0x7fff865ebff7  libsystem_c.dylib (997.90.3) <6FD3A400-4BB2-3B95-B90C-BE6E9D0D78FA> /usr/lib/system/libsystem_c.dylib
    0x7fff8660a000 -     0x7fff86633ff7  libc++abi.dylib (49.1) <21A807D3-6732-3455-B77F-743E9F916DF0> /usr/lib/libc++abi.dylib
    0x7fff8665a000 -     0x7fff86812ffb  libicucore.A.dylib (511.34) <616A65D6-3F20-3EAB-8CA8-273AD890261C> /usr/lib/libicucore.A.dylib
    0x7fff86813000 -     0x7fff86842fd2  libsystem_m.dylib (3047.16) <B7F0E2E4-2777-33FC-A787-D6430B630D54> /usr/lib/system/libsystem_m.dylib
    0x7fff87206000 -     0x7fff8720fff3  libsystem_notify.dylib (121) <52571EC3-6894-37E4-946E-064B021ED44E> /usr/lib/system/libsystem_notify.dylib
    0x7fff878c1000 -     0x7fff8790ffff  libcorecrypto.dylib (161.1) <F3973C28-14B6-3006-BB2B-00DD7F09ABC7> /usr/lib/system/libcorecrypto.dylib
    0x7fff891cc000 -     0x7fff893b1fff  com.apple.CoreFoundation (6.9 - 855.17) <729BD6DA-1F63-3E72-A148-26F21EBF52BB> /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
    0x7fff893b2000 -     0x7fff893b3ffb  libremovefile.dylib (33) <3543F917-928E-3DB2-A2F4-7AB73B4970EF> /usr/lib/system/libremovefile.dylib
    0x7fff8a7f4000 -     0x7fff8a7f6ff7  libquarantine.dylib (71) <7A1A2BCB-C03D-3A25-BFA4-3E569B2D2C38> /usr/lib/system/libquarantine.dylib
    0x7fff8b547000 -     0x7fff8b54ffff  libsystem_dnssd.dylib (522.92.1) <17B03FFD-92C5-3282-9981-EBB28B456207> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff8c3e8000 -     0x7fff8c42aff7  libauto.dylib (185.5) <F45C36E8-B606-3886-B5B1-B6745E757CA8> /usr/lib/libauto.dylib
    0x7fff8c4fb000 -     0x7fff8c522ff7  libsystem_network.dylib (241.3) <8B1E1F1D-A5CC-3BAE-8B1E-ABC84337A364> /usr/lib/system/libsystem_network.dylib
    0x7fff8c523000 -     0x7fff8c52aff8  liblaunch.dylib (842.92.1) <A40A0C7B-3216-39B4-8AE0-B5D3BAF1DA8A> /usr/lib/system/liblaunch.dylib
    0x7fff8c561000 -     0x7fff8c563ff3  libsystem_configuration.dylib (596.15) <4998CB6A-9D54-390A-9F57-5D1AC53C135C> /usr/lib/system/libsystem_configuration.dylib
    0x7fff8c7cd000 -     0x7fff8c7cefff  libsystem_sandbox.dylib (278.11.1) <0D0B13EA-6B7A-3AC8-BE60-B548543BEB77> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff8c81e000 -     0x7fff8c823fff  libmacho.dylib (845) <1D2910DF-C036-3A82-A3FD-44FF73B5FF9B> /usr/lib/system/libmacho.dylib
    0x7fff8c825000 -     0x7fff8c829ff7  libcache.dylib (62) <BDC1E65B-72A1-3DA3-A57C-B23159CAAD0B> /usr/lib/system/libcache.dylib
    0x7fff8d311000 -     0x7fff8d335fff  libxpc.dylib (300.90.2) <AB40CD57-F454-3FD4-B415-63B3C0D5C624> /usr/lib/system/libxpc.dylib
    0x7fff8d496000 -     0x7fff8d4b0fff  libdispatch.dylib (339.92.1) <C4E4A18D-3C3B-3C9C-8709-A4270D998DE7> /usr/lib/system/libdispatch.dylib
    0x7fff8e58d000 -     0x7fff8e594ff7  libsystem_pthread.dylib (53.1.4) <AB498556-B555-310E-9041-F67EC9E00E2C> /usr/lib/system/libsystem_pthread.dylib
    0x7fff90152000 -     0x7fff90159fff  libcompiler_rt.dylib (35) <4CD916B2-1B17-362A-B403-EF24A1DAC141> /usr/lib/system/libcompiler_rt.dylib
    0x7fff904dd000 -     0x7fff904ddff7  libkeymgr.dylib (28) <3AA8D85D-CF00-3BD3-A5A0-E28E1A32A6D8> /usr/lib/system/libkeymgr.dylib
    0x7fff9090d000 -     0x7fff9091eff7  libz.1.dylib (53) <42E0C8C6-CA38-3CA4-8619-D24ED5DD492E> /usr/lib/libz.1.dylib
    0x7fff91aec000 -     0x7fff91af6fff  libcommonCrypto.dylib (60049) <8C4F0CA0-389C-3EDC-B155-E62DD2187E1D> /usr/lib/system/libcommonCrypto.dylib
    0x7fff92113000 -     0x7fff92124ff7  libsystem_asl.dylib (217.1.4) <655FB343-52CF-3E2F-B14D-BEBF5AAEF94D> /usr/lib/system/libsystem_asl.dylib
    0x7fff92846000 -     0x7fff92862ff7  libsystem_kernel.dylib (2422.110.17) <873931CE-D1AF-3596-AADB-D2E63C9AB29F> /usr/lib/system/libsystem_kernel.dylib

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 76355
    thread_create: 0
    thread_set_state: 795

VM Region Summary:
ReadOnly portion of Libraries: Total=76.1M resident=112.7M(148%) swapped_out_or_unallocated=16777216.0T(23109604212736%)
Writable regions: Total=26.5M written=172K(1%) resident=236K(1%) swapped_out=0K(0%) unallocated=26.2M(99%)

REGION TYPE                      VIRTUAL
===========                      =======
Kernel Alloc Once                     4K
MALLOC                             18.2M
MALLOC (admin)                       16K
STACK GUARD                        56.0M
Stack                              8192K
VM_ALLOCATE                           8K
__DATA                             1108K
__LINKEDIT                         66.8M
__TEXT                             9540K
__UNICODE                           544K
shared memory                         4K
===========                      =======
TOTAL                             159.9M

我的主要职能:

int main(int argc, char* argv[])
{
//    if(argc != 2){
//        std::cout << "Useage: UDPSever <videohub ip> \n";
//        return 1;
//    }
    std::string videoHubIp;
    std::cout << "Enter VideoHub IP Address" << std::endl;
    std::cin >> videoHubIp;
    std::cin.ignore();

    HRESULT result = NULL;
    IVideohub *videohub;
    IVideohubDiscovery *videoHubDiscovery = CreateVideohubDiscoveryInstance(NULL);

    CFStringRef deviceAddress = CFStringCreateWithCString (NULL, videoHubIp.c_str(), kCFStringEncodingUTF8);
//    CFStringRef deviceAddress = CFStringCreateWithCString (NULL, argv[1], kCFStringEncodingUTF8);

    if((result = videoHubDiscovery->ConnectTo(deviceAddress, 10000, &videohub) != S_OK)){ //this is line 34
        std::cout << "Failed to connect, check network settings..." << std::endl;
        return 1;
    }

    std::cout << "connected" << std::endl;
    boost::asio::io_service io;
    OSCHandler osc(videohub);
    udp_server server(io, osc);
    io.run();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

事实证明,当通过Finder启动程序时,它的工作目录设置为&#39; /&#39;而不是从中启动可执行文件的目录。这导致程序中的段错误,因为我试图在可执行文件的相对位置加载库。我仍然需要弄清楚如何做到这一点,但至少现在知道错误的原因。

感谢有关日志记录和验证的建议,通过这些建议,我能够弄清楚发生了什么。

编辑:如果其他人有这个问题,我在这里找到了一个解决方案:

http://codemunch.blogspot.co.uk/2013/04/do-not-make-assumptions-on-current.html