Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:32:24+00:00 2026-06-12T08:32:24+00:00

Using boost::asio i use async_accept to accept connections. This works good, but there is

  • 0

Using boost::asio i use async_accept to accept connections. This works good, but there is one issue and i need a suggestion how to deal with it. Using typical async_accept:

  Listener::Listener(int port)
        : acceptor(io, ip::tcp::endpoint(ip::tcp::v4(), port))
        , socket(io) {
          start_accept();
  }

  void Listener::start_accept() {
      Request *r = new Request(io);
      acceptor.async_accept(r->socket(), 
        boost::bind(&Listener::handle_accept, this, r, placeholders::error));
  }

Works fine but there is a issue: Request object is created with plain new so it can memory “leak”. Not really a leak, it leaks only at program stop, but i want to make valgrind happy.

Sure there is an option: i can replace it with shared_ptr, and pass it to every event handler. This will work until program stop, when asio io_service is stopping, all objects will be destroyed and Request will be free’d. But this way i always must have an active asio event for Request, or it will be destroyed! I think its direct way to crash so i dont like this variant, too.

UPD Third variant: Listener holds list of shared_ptr to active connections. Looks great and i prefer to use this unless some better way will be found. The drawback is: since this schema allows to do “garbage collection” on idle connects, its not safe: removing connection pointer from Listener will immediately destroy it, what can lead to segfault when some of connection’s handler is active in other thread. Using mutex cant fix this cus in this case we must lock nearly anything.

Is there a way to make acceptor work with connection management some beautiful and safe way? I will be glad to hear any suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:32:26+00:00Added an answer on June 12, 2026 at 8:32 am

    The typical recipe for avoiding memory leaks when using this library is using a shared_ptr, the io_service documentation specifically mentions this

    Remarks

    The destruction sequence described above permits programs to simplify
    their resource management by using shared_ptr<>. Where an object’s
    lifetime is tied to the lifetime of a connection (or some other
    sequence of asynchronous operations), a shared_ptr to the object would
    be bound into the handlers for all asynchronous operations associated
    with it. This works as follows:

    When a single connection ends, all associated asynchronous operations
    complete. The corresponding handler objects are destroyed, and all
    shared_ptr references to the objects are destroyed. To shut down the
    whole program, the io_service function stop() is called to terminate
    any run() calls as soon as possible. The io_service destructor defined
    above destroys all handlers, causing all shared_ptr references to all
    connection objects to be destroyed.

    For your scenario, change your Listener::handle_accept() method to take a boost::shared_ptr<Request> parameter. Your second concern

    removing connection pointer from Listener will immediately destroy it,
    what can lead to segfault when some of connection’s handler is active
    in other thread. Using mutex cant fix this cus in this case we must
    lock nearly anything.

    is mitigated by inheriting from the boost::enable_shared_from_this template in your classes:

    class Listener : public boost::enable_shared_from_this<Listener>
    {
       ...
    };
    

    then when you dispatch handlers, use shared_from_this() instead of this when binding to member functions of Listener.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using a boost::asio::ip::tcp:socket there are many methods one can use to write/send data.
I'm writing a performance-critical bidirectional streaming server using boost.asio. The server works this way
I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
I have successfully implemented a network application in visual CLR project using boost.asio. but
Unlike this question: Linker Error while building application using Boost Asio in Visual Studio
I am using Boost asio 1.48. Is it possible to re-use a single boost::asio::io_service
I am learning how to use the boost asio libraries and I am using
There is a client/server application written using Boost's ASIO (Boost v.1.48) + OpenSSL (v.1.0.0d).
I am trying to adapt one of the boost::asio examples to use c++11 /
I am using boost::asio::ip::udp::socket to communicate. I use socket.receive_from(...) to receive messages from clients.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.