Discussion:
[asio-users] Crash inside asio while creating acceptor.
Przemysław Romaniak
2014-04-29 20:39:34 UTC
Permalink
Hello.
I'm trying to use asio library (without boost) to create a simple chat
server (yes, I've seen examples). Unfortunately I've experienced
problems just at the beginning.

I'm using MinGW 4.8.2 included in QT 5.2.1 Windows package and Asio
1.10.1 with ASIO_STANDALONE flag.

Now excuse me showing a little too much code, but it seems to crash
strictly in that combination.

I've created simple class Service:

class Service {

friend class Listener;

public:
Service();
void Listen(Uint32 port);
...

protected:
asio::io_service m_service;
std::list<std::unique_ptr<Listener>> m_listeners;

};

So asio::io_service is kept as a member variable.

Constructor is currently empty. There is a method called Listen to
create new instance of Listener:

void Service::Listen(Uint32 port) {
m_listeners.push_back(std::unique_ptr<Listener>(new Listener(port, *this)));
}


Listener class looks like that:

class Listener {
friend class Service;
private:
Listener(Uint32 port, Service &service);
Uint32 m_port;
...
tcp::endpoint m_endpoint;
tcp::acceptor m_acceptor;
Service &m_service;
};


And has constructor:

Listener::Listener(Uint32 port, Service &service)
: m_service(service), m_port(port), m_endpoint(tcp::v4(), port),
m_acceptor(service.m_service, m_endpoint) {

...async_listen etc

}

Unfortunately, as soon as I call Service->Listen I'm getting segfault
inside asio::detail::service_registry::keys_match. It's related to
creation of m_acceptor in Listener constructor.

It seems to me that while io_service and service_registry_ inside
points to correct memory, first_service_ gets somewhat corrupted and
is pointing to 0xffffffff. owner_ also points to 0xffffffff.
I deduced io_service and service_registry are correct by printing &o
and "this" in service_registry constructor from
asio/detail/impl/service_registry.hpp . They are the same as ones when
crash occur.

Service instance is on heap, so I'm sure it isn't destroyed prematurely.

When I'm pretty sure I'm doing something completely wrong, that code
seems to work correctly on Linux....


I can provide any information needed to debug.
Thank you for any help.

Loading...