在终端中获取分段故障11错误
Getting segmentation fault 11 error in terminal
我有一个聊天应用程序。
在这种情况下,每当我尝试启动一个提供用户名的聊天连接时,它都会正确启动,并开始监听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
我不知道这意味着什么。
看起来您可能正在传递一个NULL指针作为该函数的字符串参数。我建议在调用strtol_l之前检查该字符串变量,并确保将其设置为有效的非null字符串值。
更新:你已经从问题中删除了你的代码,我记不清它是什么了,但它有点像:
recvdTokens = strtok(something, ";;;");
client.receivedSeqNum = atoi(recvdTokens);
你应该做一些类似的事情:
recvdTokens = strtok(something, ";;;");
if (recvdTokens) {
// token found
client.receivedSeqNum = atoi(recvdTokens);
}
您可能还需要添加一个else
部分来处理未找到令牌的情况。
相关文章:
- Arch Linux.AUR 包 mysql 不能用 makepkg 构建.错误:构建 () 中出现故障
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 在执行strcpy时获取分割故障错误
- 使用许多嵌套的#include语句对编译器错误进行故障排除
- 分割故障/错误读数变量-C 列表
- 使用长变量时分割故障(11)错误
- G :内部编译器错误:分割故障(程序CC1PLUS) - 我在哪里开始
- 二进制搜索树操作程序错误:分割故障(核心倾倒)
- [[弃用]]导致VS2017(错误C4996)中的构建故障
- 实例化对象时如何修复分段故障错误
- 分割故障错误
- 为什么std::istringstream在没有引发故障位标志的情况下生成错误的结果
- C/C++ 字符串错误与 ARM SEG 故障总线错误
- 浮点异常,分割故障和类似的错误
- C++段故障错误
- 基本阵列操作中的分段故障错误
- 故障错误解决
- 给出分段故障错误的c++ LCA程序
- 分段故障错误(信号名称:SIGSEGV)的原因是什么,以及如何查找/修复它
- 奇怪的隔离故障错误