无法使用提升线程找出分段错误

时间:2014-01-26 19:45:18

标签: c++ multithreading boost segmentation-fault valgrind

我正在使用针对套接字和线程的boost库在c ++中开发一个stomp客户端。 该程序包括两个主要类SocketListener,它从套接字获取帧,UserInterface接收来自用户的命令并将帧发送到服务器。 我无法弄清楚当我关闭SocketListener时得到的分段错误。 (当我调用关机方法时) 使用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.

这是代码:

main.cpp:

main() {
    StompClient* client = new StompClient();
    userInterface* ui = new userInterface(client); // this doesnt invoke the segfault but SocketListener::shutdown() is called from this object
    SocketListener* socketListener = new SocketListener(client,ui);

    boost::thread* uiThread = new boost::thread(&userInterface::run, ui); 
    boost::thread* socketThread= new boost::thread(&SocketListener::run, socketListener);

    socketThread->join();
    uiThread->join();

} 

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
}

0 个答案:

没有答案
相关问题