我创建了两个类=>超类:TcpServer子类:AccountServer 当我在AccountServer中分配接受器的共享指针时,它崩溃了。只有当acceptor是TcpServer的数据成员时,才会出现这种情况,如果我将acceptor放入AccountServer,那很好。下面有一些代码和信息,希望有人能告诉我原因,不胜感激。
class TcpServer
{
public:
typedef boost::shared_ptr<boost::asio::io_service> ServicePtr;
**typedef boost::shared_ptr<boost::asio::ip::tcp::acceptor> AcceptorPtr;**
typedef boost::shared_ptr<boost::asio::ip::tcp::endpoint> EndpointPtr;
TcpServer();
~TcpServer();
void init();
protected:
EndpointPtr m_pEndpoint;
ServicePtr m_pService;
**AcceptorPtr m_pAcceptor;**
};
class AccountServer : public TcpServer
{
public:
**typedef boost::shared_ptr<boost::asio::ip::tcp::acceptor> AcceptorPtr;**
AccountServer();
~AccountServer();
void init();
private:
Json::Value m_AccountConf;
**//AcceptorPtr m_pAcceptor;** //delete this line so that the m_pAcceptor in the
//super class will be used in the runtime
};
void my::AccountServer::init()
{
int port = 10000;
m_pService = ServicePtr(new boost::asio::io_service());
m_pEndpoint = EndpointPtr(new boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
**m_pAcceptor = AcceptorPtr(new boost::asio::ip::tcp::acceptor(*m_pService, *m_pEndpoint)); // here is where it crashed**
...other codes
}
我认为接受器一旦被分配就会被破坏,但为什么,以及它为什么会崩溃。我现在完全糊涂了。如果你们中任何人对这个问题一无所知,那么非常欢迎你们告诉我,那将是非常感激的。 :)