簡體   English   中英

boost :: asio :: ip :: tcp :: acceptor上的分段錯誤

[英]Segmentation fault on boost::asio::ip::tcp::acceptor

我從pthread_mutex_lock獲取分段錯誤,這是我的回溯信息:

    Program received signal SIGSEGV, Segmentation fault.
0x00007ffff61b03f0 in pthread_mutex_lock () from /lib/libpthread.so.0
(gdb) bt
#0  0x00007ffff61b03f0 in pthread_mutex_lock () from /lib/libpthread.so.0
#1  0x000000000041141f in boost::asio::detail::posix_mutex::lock (this=0x1)
    at /usr/include/boost/asio/detail/posix_mutex.hpp:71
#2  0x00000000004242ee in boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> >::basic_socket_acceptor(boost::asio::io_service&, boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> const&, bool) ()
#3  0x000000000041c7df in Server (this=0x7fffffffe210) at src/server.cpp:12
#4  0x00000000004280bd in main () at src/main.cpp:21
(gdb) 

我的代碼:

#include "server.h"

#include "client.h"



#include <iostream>

#include <boost/thread.hpp>

#include <boost/bind.hpp>



#include "defines.h"



#include "server.h"
#include "client.h"

#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>

#include "defines.h"

Irc::Server::Server()
{
    //
}

Irc::Server::~Server()
{
    m_clients.clear();
}

void Irc::Server::start()
{
    m_acceptor = new boost::asio::ip::tcp::acceptor(io);
}

void Irc::Server::startAccept()
{
    SocketPtr p(new boost::asio::ip::tcp::socket(io));
    boost::asio::ip::tcp::resolver resolver(io);
    boost::asio::ip::tcp::resolver::query query(SERVER_ADDRESS, SERVER_PORT);
    boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
    m_acceptor->open(endpoint.protocol());
    m_acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
    m_acceptor->bind(endpoint);
    m_acceptor->listen();
    m_acceptor->async_accept(*p,
        boost::bind(&Irc::Server::clientHandler, this, 
        p));
}

void Irc::Server::clientHandler(SocketPtr newSocket)
{
    typedef boost::shared_ptr<boost::thread> ThreadsPool;
    typedef std::vector<ThreadsPool> Threads;
    Threads threads;
    for (int i = 0; i < 2; ++i) {
        ThreadsPool p(new boost::thread(boost::bind(
            &boost::asio::io_service::run, &io)));
        threads.push_back(p);
    }
    for (Threads::iterator it = threads.begin(); it != threads.end(); ++it)
        (*it)->join();

    ClientPtr p(new Client(newSocket));
    m_clients[newSocket] = p;
    SocketPtr new_(new boost::asio::ip::tcp::socket(io));
    m_acceptor->async_accept(*new_,
        boost::bind(&Irc::Server::clientHandler, this, new_));
}


#ifndef SERVER_H
#define SERVER_H

namespace Irc
{
    class Client;
    class Server;
} //namespace Irc

#include <boost/asio.hpp>
#include <map>

typedef boost::shared_ptr<Irc::Client> ClientPtr;
typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SocketPtr;
typedef std::map<SocketPtr, ClientPtr> ClientsMap;

class Irc::Server
{
    public:
        Server();
        ~Server();

        void start();

        void startAccept();

        void clientHandler(SocketPtr);

        ClientsMap::iterator begin() { return m_clients.begin(); }
        ClientsMap::iterator end() { return m_clients.end(); }

    private:
        boost::asio::ip::tcp::acceptor* m_acceptor;
        boost::asio::io_service io;
        ClientsMap m_clients;
};
#endif

您需要發布更多代碼(Server :: Server())並將文件發布在單獨的代碼塊中以提高可讀性。

嘗試將io更改為指針,並在構造函數中顯式初始化它:

Irc::Server::Server() : io()
{
    start();
    startAccept();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM