1

This is my first project with boost::asio and I´m building an async server.

I need to use a shared_ptr to control access to the session control pointer. Here is the way I´m doing to define the session and pass it to the boost async function:

// Define the new session object
std::shared_ptr<SocketSession> session = std::make_shared<SocketSession>(ioService);

// Configure the acceptor
acceptor.async_accept(session->getSessionSocket(),
                      boost::bind(&SocketServer::HandleAccept, 
                      this, 
                      session, 
                      boost::asio::placeholders::error));

And the handler signature:

void SocketServer::HandleAccept(std::shared_ptr<SocketSession> session,
                                const boost::system::error_code& errorCode)

The code compile fine, not errors at all. But on running the code, the HandleAccept method is called with an error: Operation canceled, not even listening to the socket.

It seens that this is related to the way I´m using shrared_ptr. I´ve gone through some examples and boost uses shared_from_this. I can´t find out why is that necessary, and why my shared_ptr fail at runtime.

Help appreciated.

Note: This code is running on ther server class (Server), not on the session class (SocketSession). In my view this lead not to use shared_from_this as SocketSession is being created at another object....

Mendes
  • 17,489
  • 35
  • 150
  • 263
  • If you didn't, I suggest checking the `boost::asio` server example at http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp – Francis Straccia Dec 01 '15 at 14:59
  • I´ve checked already, but boost docs does not explain the fundamentals of why is it being used - it just says how it is used. My concept using `std::shared_ptr` seens to be right, but I don´t understand why is it not working... – Mendes Dec 01 '15 at 15:01
  • @Mendez Are you saying that the boost example doesn't work? Are you saying that you changed how the boost example works, and it no longer works? If so, can you highlight what you changed? Or are you saying you generated a new pattern, and that new pattern didn't work? I'm just trying to reduce the difficulty of working out what went wrong. Also, a minimal self contained compiling example would be useful. See https://stackoverflow.com/help/mcve – Yakk - Adam Nevraumont Dec 01 '15 at 15:07
  • 1
    In short: "Are you saying that the boost example doesn't work?" No. It works. "Are you saying that you changed how the boost example works, and it no longer works?" A sort of. I took boost example, and added the `shared_ptr´s` to keep control of the session class, because later I will add a timeout. My full code is here: http://stackoverflow.com/questions/34019004/c-boostbind-with-stdshared-ptr-trowing-operation-canceled-on-timeout-handl but I tried here to focus on what is going wrong. – Mendes Dec 01 '15 at 15:12
  • By the way, exachangind the `shared_ptr` to normal pointers `SocketSession* session = new SocketSession` works fine. I´m just getting this error because I need to use `shared_ptrs` due to later `delete` on timeout... – Mendes Dec 01 '15 at 15:12
  • Small tip: you should do `std::shared_ptr session = std::make_shared(ioService)`. Slightly more concise and efficient. – John Zwinck Dec 01 '15 at 15:21

1 Answers1

2

@Mendez I have several introductory samples when people ran into the requirement for this "ASIO pattern".

You could look at them, because I do explain the pattern and why it's introduced:

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks for the posts. Very usefull, but doesn´t match exactly what I´m tryind to do. In your post you create several servers. In my case I have one server and several sessions created on connection requests... From the posts I could change to session class to derive from `enable_shared_from_this` and the original error is gone... Now I´m fighting against another problem: What to do with the `server` class when I delete the `session` class due to timeout.... I will post that in another SO thread to clarify. Thanks for helping. – Mendes Dec 01 '15 at 15:59
  • @Mendez I'm absolutely sure I have multiple answers showing your _exact_ situation. I was pretty sure I linked one of them. But in case you need more: https://stackoverflow.com/search?q=user%3A85371+asio+shared_from_this – sehe Dec 01 '15 at 16:03