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

Getting segmentation fault 11 error in terminal

本文关键字:故障 错误 分段 获取 终端      更新时间:2023-10-16

我有一个聊天应用程序。

在这种情况下,每当我尝试启动一个提供用户名的聊天连接时,它都会正确启动,并开始监听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

我不知道这意味着什么。

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

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

更新:你已经从问题中删除了你的代码,我记不清它是什么了,但它有点像:

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

你应该做一些类似的事情:

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

您可能还需要添加一个else部分来处理未找到令牌的情况。