在终端中获取分段错误11错误

时间:2015-02-27 06:42:35

标签: c++ segmentation-fault

我有一个聊天应用程序。

每当我尝试开始提供用户名的聊天连接时,它会正常启动并开始侦听IP地址。但是,每当我尝试使用主用户的IP地址将另一个用户添加到该连接时,我在该终端上获得Segmentation Fault: 11,并且

NOTICE abc has joined on 10.0.0.2:52332

NOTICE abc has left chat or crashed
同时在主用户终端上

。为什么会这样?任何人都可以帮我解决这个问题吗?

编辑: 当我用valgrind运行它时,我得到以下结果:

==22843== Memcheck, a memory error detector
==22843== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==22843== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==22843== Command: ./a.out abc 10.0.0.2:61488
==22843== 
--22843-- UNKNOWN host message [id 412, to mach_host_self(), reply 0x30f]
--22843-- UNKNOWN host message [id 222, to mach_host_self(), reply 0x30f]
--22843-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--22843-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--22843-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
--22843-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 8 times)
==22843== Invalid read of size 1
==22843==    at 0x100341879: strtol_l (in /usr/lib/system/libsystem_c.dylib)
==22843==    by 0x10002222A: main (chat.cpp:1765)
==22843==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==22843== 
==22843== 
==22843== Process terminating with default action of signal 11 (SIGSEGV)
==22843==  Access not within mapped region at address 0x0
==22843==    at 0x100341879: strtol_l (in /usr/lib/system/libsystem_c.dylib)
==22843==    by 0x10002222A: main (chat.cpp:1765)
==22843==  If you believe this happened as a result of a stack
==22843==  overflow in your program's main thread (unlikely but
==22843==  possible), you can try to increase the size of the
==22843==  main thread stack using the --main-stacksize= flag.
==22843==  The main thread stack size used in this run was 8388608.
==22843== 
==22843== HEAP SUMMARY:
==22843==     in use at exit: 46,007 bytes in 485 blocks
==22843==   total heap usage: 599 allocs, 114 frees, 61,582 bytes allocated
==22843== 
==22843== LEAK SUMMARY:
==22843==    definitely lost: 128 bytes in 2 blocks
==22843==    indirectly lost: 0 bytes in 0 blocks
==22843==      possibly lost: 14,034 bytes in 123 blocks
==22843==    still reachable: 31,845 bytes in 360 blocks
==22843==         suppressed: 0 bytes in 0 blocks
==22843== Rerun with --leak-check=full to see details of leaked memory
==22843== 
==22843== For counts of detected and suppressed errors, rerun with: -v
==22843== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Killed: 9

我不知道这意味着什么。

1 个答案:

答案 0 :(得分:1)

在调用strtol_l函数时,Valgrind在chat.cpp的第1765行指出了一个问题。

看起来您可能正在传入一个NULL指针作为该函数的字符串参数。我建议在调用strtol_l之前检查该字符串变量并确保将其设置为有效的非空字符串值。

<强>更新 你已经从问题中删除了你的代码,我不记得究竟是什么,但它是这样的:

recvdTokens = strtok(something, ";;;");
client.receivedSeqNum = atoi(recvdTokens);

你应该做类似的事情:

recvdTokens = strtok(something, ";;;");
if (recvdTokens) {
  // token found
  client.receivedSeqNum = atoi(recvdTokens);
}

如果找不到令牌,您可能还需要添加else部分来处理此案例。