Boost::Asio crashes all the time when starting multiple connections

Hi, I have started to write a ProxyServer that analyzes TDS-packages sent from the Microsoft SQLServer Management Studio to a MS SQLServer. It seems to work fine for one or two connections, but if more connections are openend, boost suddenly crashes, mostly in the bind-template.hpp: template<class A1, class A2> result_type operator()(A1 const & a1, A2 const & a2) { list2<A1 const &, A2 const &> a(a1, a2); CRASH ===> BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0); } I have modified the code from the examples page: http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/echo/async_... I only added a second socket in the session class for handling the connection to the SQLServer. When I start SSMS and click on "New query", I can see two additional connections being opened, and then the program crashes. I have posted the source code here: session.h : http://pastebin.com/eC5fn7U8 session.cpp : http://pastebin.com/LDnQqCNr server.h: http://pastebin.com/1fDJU6sx server.cpp: http://pastebin.com/seMAVx4D ProxyServer.cpp: http://pastebin.com/8S7YfE8L Am I doing something wrong? Maybe the code isn't threadsafe? I'm new to boost::asio and I might have missed an important point here. Maybe I need more than one io_service object? Andi -- Using Opera's revolutionary email client: http://www.opera.com/mail/

It seems to work fine for one or two connections, but if more connections are openend, boost suddenly crashes, mostly in the bind-template.hpp:
To be more exact, you application crashes ;).
template<class A1, class A2> result_type operator()(A1 const & a1, A2 const & a2) { list2<A1 const &, A2 const &> a(a1, a2); CRASH ===> BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0); }
This is an invocation of some binder. In your case it's an invocation of a completion handler. It crashes because it's bound to a dead (i.e. deleted) object - which is apparently reasonable, since you don't use shared_from_this() idiom. Specifically in your code the problem is the following: 1) You issue 2 async.requests: socket_.async_read_some(..., boost::bind(&session::handle_read, this,...)); sqlsocket_.async_read_some(..., boost::bind(&session::handle_sqlserver_read, this, ...)); 2) One of the requests ends with an error (even just EOF), and you delete the connection object. 3) Now the completion handler of the seconds request is being invoked... crash. Conclusion: do not bind your handlers to "this", do not delete objects manually. Instead, use shared_from_this() idiom.

Ah I see, so I'm working on a "dead" object, sure this must fail :-) I took a look at the connectionManager class in the HTTP server example, I guess this is what I need. Thanks for your fast and informative answer! Andi On Tue, 14 Jun 2011 12:28:02 +0200, Igor R <boost.lists@gmail.com> wrote:
It seems to work fine for one or two connections, but if more connections are openend, boost suddenly crashes, mostly in the bind-template.hpp:
To be more exact, you application crashes ;).
template<class A1, class A2> result_type operator()(A1 const & a1, A2 const & a2) { list2<A1 const &, A2 const &> a(a1, a2); CRASH ===> BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0); }
This is an invocation of some binder. In your case it's an invocation of a completion handler. It crashes because it's bound to a dead (i.e. deleted) object - which is apparently reasonable, since you don't use shared_from_this() idiom. Specifically in your code the problem is the following: 1) You issue 2 async.requests: socket_.async_read_some(..., boost::bind(&session::handle_read, this,...)); sqlsocket_.async_read_some(..., boost::bind(&session::handle_sqlserver_read, this, ...)); 2) One of the requests ends with an error (even just EOF), and you delete the connection object. 3) Now the completion handler of the seconds request is being invoked... crash.
Conclusion: do not bind your handlers to "this", do not delete objects manually. Instead, use shared_from_this() idiom. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Using Opera's revolutionary email client: http://www.opera.com/mail/
participants (2)
-
Andi Clemens
-
Igor R