无法使用boost线程找出分段错误 我在C++中使用STOP库开发线程和线程的STOMP客户端。 该程序由两个主要类组成:SocketListener和UserInterface。SocketListener从套接字获取帧,UserInterface从用户接收命令并向服务器发送帧。 我无法找出在关闭SocketListener时出现的分段错误。(当我调用shutdown方法时) 使用valgrind,我得到以下错误: ==8450== Thread 2: ==8450== Invalid read of size 8 ==8450== at 0x588AF8B: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16) ==8450== by 0x417846: SocketListener::shutdown(std::string) (SocketListener.cpp:114) ==8450== by 0x41D2A7: userInterface::shutdown() (UserInterface.cpp:123) ==8450== by 0x41C7ED: userInterface::run() (UserInterface.cpp:37) ==8450== by 0x415F0B: boost::_mfi::mf0<void, userInterface>::operator()(userInterface*) const (mem_fn_template.hpp:49) ==8450== by 0x415DF1: void boost::_bi::list1<boost::_bi::value<userInterface*> >::operator()<boost::_mfi::mf0<void, userInterface>, boost::_bi::list0>(boost::_bi::type<void>, boost::_mfi::mf0<void, userInterface>&, boost::_bi::list0&, int) (bind.hpp:253) ==8450== by 0x415B4E: boost::_bi::bind_t<void, boost::_mfi::mf0<void, userInterface>, boost::_bi::list1<boost::_bi::value<userInterface*> > >::operator()() (bind_template.hpp:20) ==8450== by 0x4157E7: boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, userInterface>, boost::_bi::list1<boost::_bi::value<userInterface*> > > >::run() (thread.hpp:74) ==8450== by 0x5046C2C: thread_proxy (in /usr/local/boost/1.51.0/lib/libboost_thread.so.1.51.0) ==8450== by 0x5260E99: start_thread (pthread_create.c:308) ==8450== by 0x5DF53FC: clone (clone.S:112) ==8450== Address 0x50 is not stack'd, malloc'd or (recently) free'd ==8450== ==8450== ==8450== Process terminating with default action of signal 11 (SIGSEGV) ==8450== Access not within mapped region at address 0x50 ==8450== at 0x588AF8B: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16) ==8450== by 0x417846: SocketListener::shutdown(std::string) (SocketListener.cpp:114) ==8450== by 0x41D2A7: userInterface::shutdown() (UserInterface.cpp:123) ==8450== by 0x41C7ED: userInterface::run() (UserInterface.cpp:37) ==8450== by 0x415F0B: boost::_mfi::mf0<void, userInterface>::operator()(userInterface*) const (mem_fn_template.hpp:49) ==8450== by 0x415DF1: void boost::_bi::list1<boost::_bi::value<userInterface*> >::operator()<boost::_mfi::mf0<void, userInterface>, boost::_bi::list0>(boost::_bi::type<void>, boost::_mfi::mf0<void, userInterface>&, boost::_bi::list0&, int) (bind.hpp:253) ==8450== by 0x415B4E: boost::_bi::bind_t<void, boost::_mfi::mf0<void, userInterface>, boost::_bi::list1<boost::_bi::value<userInterface*> > >::operator()() (bind_template.hpp:20) ==8450== by 0x4157E7: boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, userInterface>, boost::_bi::list1<boost::_bi::value<userInterface*> > > >::run() (thread.hpp:74) ==8450== by 0x5046C2C: thread_proxy (in /usr/local/boost/1.51.0/lib/libboost_thread.so.1.51.0) ==8450== by 0x5260E99: start_thread (pthread_create.c:308) ==8450== by 0x5DF53FC: clone (clone.S:112) ==8450== If you believe this happened as a result of a stack ==8450== overflow in your program's main thread (unlikely but ==8450== possible), you can try to increase the size of the ==8450== main thread stack using the --main-stacksize= flag. ==8450== The main thread stack size used in this run was 8388608.

无法使用boost线程找出分段错误 我在C++中使用STOP库开发线程和线程的STOMP客户端。 该程序由两个主要类组成:SocketListener和UserInterface。SocketListener从套接字获取帧,UserInterface从用户接收命令并向服务器发送帧。 我无法找出在关闭SocketListener时出现的分段错误。(当我调用shutdown方法时) 使用valgrind,我得到以下错误: ==8450== Thread 2: ==8450== Invalid read of size 8 ==8450== at 0x588AF8B: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16) ==8450== by 0x417846: SocketListener::shutdown(std::string) (SocketListener.cpp:114) ==8450== by 0x41D2A7: userInterface::shutdown() (UserInterface.cpp:123) ==8450== by 0x41C7ED: userInterface::run() (UserInterface.cpp:37) ==8450== by 0x415F0B: boost::_mfi::mf0<void, userInterface>::operator()(userInterface*) const (mem_fn_template.hpp:49) ==8450== by 0x415DF1: void boost::_bi::list1<boost::_bi::value<userInterface*> >::operator()<boost::_mfi::mf0<void, userInterface>, boost::_bi::list0>(boost::_bi::type<void>, boost::_mfi::mf0<void, userInterface>&, boost::_bi::list0&, int) (bind.hpp:253) ==8450== by 0x415B4E: boost::_bi::bind_t<void, boost::_mfi::mf0<void, userInterface>, boost::_bi::list1<boost::_bi::value<userInterface*> > >::operator()() (bind_template.hpp:20) ==8450== by 0x4157E7: boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, userInterface>, boost::_bi::list1<boost::_bi::value<userInterface*> > > >::run() (thread.hpp:74) ==8450== by 0x5046C2C: thread_proxy (in /usr/local/boost/1.51.0/lib/libboost_thread.so.1.51.0) ==8450== by 0x5260E99: start_thread (pthread_create.c:308) ==8450== by 0x5DF53FC: clone (clone.S:112) ==8450== Address 0x50 is not stack'd, malloc'd or (recently) free'd ==8450== ==8450== ==8450== Process terminating with default action of signal 11 (SIGSEGV) ==8450== Access not within mapped region at address 0x50 ==8450== at 0x588AF8B: std::string::assign(std::string const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16) ==8450== by 0x417846: SocketListener::shutdown(std::string) (SocketListener.cpp:114) ==8450== by 0x41D2A7: userInterface::shutdown() (UserInterface.cpp:123) ==8450== by 0x41C7ED: userInterface::run() (UserInterface.cpp:37) ==8450== by 0x415F0B: boost::_mfi::mf0<void, userInterface>::operator()(userInterface*) const (mem_fn_template.hpp:49) ==8450== by 0x415DF1: void boost::_bi::list1<boost::_bi::value<userInterface*> >::operator()<boost::_mfi::mf0<void, userInterface>, boost::_bi::list0>(boost::_bi::type<void>, boost::_mfi::mf0<void, userInterface>&, boost::_bi::list0&, int) (bind.hpp:253) ==8450== by 0x415B4E: boost::_bi::bind_t<void, boost::_mfi::mf0<void, userInterface>, boost::_bi::list1<boost::_bi::value<userInterface*> > >::operator()() (bind_template.hpp:20) ==8450== by 0x4157E7: boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, userInterface>, boost::_bi::list1<boost::_bi::value<userInterface*> > > >::run() (thread.hpp:74) ==8450== by 0x5046C2C: thread_proxy (in /usr/local/boost/1.51.0/lib/libboost_thread.so.1.51.0) ==8450== by 0x5260E99: start_thread (pthread_create.c:308) ==8450== by 0x5DF53FC: clone (clone.S:112) ==8450== If you believe this happened as a result of a stack ==8450== overflow in your program's main thread (unlikely but ==8450== possible), you can try to increase the size of the ==8450== main thread stack using the --main-stacksize= flag. ==8450== The main thread stack size used in this run was 8388608.,c++,multithreading,boost,segmentation-fault,valgrind,C++,Multithreading,Boost,Segmentation Fault,Valgrind,SocketListener.h: class SocketListener { public: SocketListener(StompClient* client, userInterface* ui); virtual ~SocketListener(); void run(); void exportHtml(); void processMessage(StompFrame* frame); void shutdown(string* file

SocketListener.h:

class SocketListener {

public:
    SocketListener(StompClient* client, userInterface* ui);
    virtual ~SocketListener();
    void run();
void exportHtml();
    void processMessage(StompFrame* frame);
    void shutdown(string* fileName);

private:
    StompClient* _client;
    bool _shutdown;
    userInterface* _UI;
    string _userName;
};
SocketLisener.cpp:

void SocketListener::run() {
    StompParser parser;
    StompFrame* frame = 0;
    while (!_shutdown) {
        if (frame != 0) {
            delete frame;
        }
        frame = _client->getFrame();
        if (frame == 0) {
            continue;
        }
        //switch on all the messages cases. the problem happens regardless of message type, even when not receiving anything.
}


void SocketListener::exportHtml() {
    //writing to file using poco logger
}

void SocketListener::shutdown(string* fileName) {
    cout << "shutting down socket listener" <<endl;
    _userName = *fileName; // this is line 114 which valgrind points to
    _shutdown = true;
    cout << "shutting down socket listener2" <<endl; // this line never gets printed
}
void SocketListener::run(){
StompParser解析器;
StompFrame*frame=0;
而(!\u关机){
如果(帧!=0){
删除帧;
}
frame=_client->getFrame();
如果(帧==0){
继续;
}
//打开所有消息案例。无论消息类型如何,即使没有收到任何消息,问题也会发生。
}
void SocketListener::exportHtml(){
//使用poco记录器写入文件
}
void SocketListener::shutdown(字符串*文件名){

你能从析构函数调用SocketListener::shutdown吗?
userInterface::shutdown()
看起来像什么?@zmb-\u SocketListener->shutdown(\u userName);并更改一个布尔字段(在UI对象中)
SocketListener::shutdown
被传递一个空指针。@Tom然后你的SocketListener对象被称为shutdown上的(..)可能为空或已被删除。(stacktrace显示shutdown()是从
UserInterface.cpp:123
调用的,但此处至少没有显示代码将您在main()中创建的socketListener对象分配给
ui
对象)
void SocketListener::run() {
    StompParser parser;
    StompFrame* frame = 0;
    while (!_shutdown) {
        if (frame != 0) {
            delete frame;
        }
        frame = _client->getFrame();
        if (frame == 0) {
            continue;
        }
        //switch on all the messages cases. the problem happens regardless of message type, even when not receiving anything.
}


void SocketListener::exportHtml() {
    //writing to file using poco logger
}

void SocketListener::shutdown(string* fileName) {
    cout << "shutting down socket listener" <<endl;
    _userName = *fileName; // this is line 114 which valgrind points to
    _shutdown = true;
    cout << "shutting down socket listener2" <<endl; // this line never gets printed
}