std::string 复制构造函数段错误
构建结构时会发生这种情况,我的代码: http://wklej.org/hash/c42680a7f9d/txt/ 在这一行:http://wklej.org/hash/5fefcecc371/txt/ 回溯:http://wklej.org/id/451070/txt/ 任何帮助表示赞赏 抱歉,我无法复制此处的代码,因此我将其发布到任何其他网站;(
this happens when constructing a structure, my code:
http://wklej.org/hash/c42680a7f9d/txt/ at this line: http://wklej.org/hash/5fefcecc371/txt/ backtrace: http://wklej.org/id/451070/txt/
any help is apperciated
Sorry, i fail to copy the code here so i post it at any other site ;(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用调试器并获取堆栈跟踪。
几乎可以肯定,问题是向 std::string 构造函数传递了错误的 C 字符串。也许指针无效或者 C 字符串没有终止并且构造函数读入受保护的内存。
但如果没有更多信息,我无法说出错误是什么。调试器应该立即指出这一点。
另外,您的 Socket 持有一个指针,但只定义了构造函数和析构函数。您还需要一个复制构造函数和一个赋值运算符。如果这两个操作不应该发生,则将它们定义为
private
而不进行任何实现。另外,我从你的回溯中看到这是 GCC 的旧版本。此版本可能没有使 std::string 在多线程程序中安全使用的修复程序。我不知道它何时被修复,但一些旧版本的 libstdc++ 库没有锁定字符串上的引用计数,并且当不同线程在写入字符串的同时释放字符串内存时可能会崩溃。
Use a debugger and get a stack trace.
The problem is almost certainly passing a bad C string into the std::string constructor. Perhaps the pointer is invalid or the C string does not terminate and the constructor reads off into protected memory.
But without more information I can't tell what the bug is. The debugger should point it out immediately.
Also, your
Socket
holds a pointer but only defines a constructor and destructor. You also need a copy constructor and an assignment operator. If those two operations aren't supposed to happen then define them asprivate
with no implementation.Also, I see from your backtrace that this is an old version of GCC. It's possible that this version does not have the fixes that make std::string safe to use in multi-threaded programs. I don't know when it was fixed but some older versions of the libstdc++ library didn't lock the reference counts on the string and could crash when different threads would free the string memory while also writing to it.
我已将您的代码放在这里以便能够对其进行编辑:
I've placed your code here in order to be able to edit it:
我不认为这是您唯一的问题,但在
handler
的这段代码中,您正在堆栈上创建Socket
对象。您创建的每个 Socket 对象都将在 for 循环结束时被销毁。这意味着sockets
向量中的对象是无效对象。这样做还可能会损坏内存堆,从而产生您所看到的错误。将其更改为:
I don't think this is your only problem, but in this snippet in
handler
, you're creating yourSocket
objects on the stack. EachSocket
object that you create will be destroyed at the end of thefor
loop. This means that the objects in thesockets
vector are invalid objects. Doing this may also corrupt the memory heap enough to produce the error that you're seeing.Change this to:
这个答案最初是对Dan的答案的评论,但在查看之后你的代码我觉得有必要给出一个完整的答案。您确实需要仔细查看 Boost。 Asio 示例并了解它们是如何工作的。请特别注意异步示例,看起来您还没有掌握对象生命周期以及处理程序如何工作的概念。特别是,在跳入多线程之前,您应该掌握单线程程序。当您克服了这个问题后,您应该使用一个线程池来调用
io_service::run
,而不是每个线程调用io_service
。它最终将使您的程序逻辑更容易理解。您还应该查看 valgrind,您的代码中有很多错误,如下所示:
This answer was originally a comment to Dan's answer, but after looking at your code I felt compelled to give a full answer. You really need to take a closer look at the Boost.Asio examples and understand how they work. Pay special attention to the asynchronous examples, it doesn't look like you've grasped the concepts of object lifetime and how the handlers work. Particularly, you should master single threaded programs before jumping into multiple threads. When you've conquered that, you should use a thread pool invoking
io_service::run
instead of anio_service
per thread. It will ultimately make your program's logic easier to understand.You also should look into valgrind, there's a slew of errors in your code like this one: