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

  • Home
  • SEARCH
  • 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 8299323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:06:28+00:00 2026-06-08T16:06:28+00:00

I’m working with boost::asio. I wrote a class responsible for asynchronous reading from socket.

  • 0

I’m working with boost::asio. I wrote a class responsible for asynchronous reading from socket.
In my application io_service can be stopped and started many times during one application run.
So I have to worry about memory leakage when service is stopped.
I came to two solutions:

  1. A class asking for asynchronous request provides function with a buffer for using in asio reads, and it is responsible for its freeing. This is an obvious solution but I don’t like it. Passing a parameter you don’t need to a function looks realy strange.

  2. Smart pointer binded to a callback. Example here:
    http://pastebin.com/p8nQ5NFi

Now I’m using the second solution but however I’m feeling, I’m inventing a wheel. What’s a common practice for buffer cleanage in asynchronous call? Are there any hidden problems in my aproach?

  • 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-08T16:06:30+00:00Added an answer on June 8, 2026 at 4:06 pm

    The shared_ptr approach is fairly common. However, instead of passing a shared_ptr as an additional argument to bind, it is possible to pass the shared_ptr as the instance object in place of this.

    boost::shared_ptr< my_class > ptr( this );
    
    boost::asio::async_read( stream, buffer, 
      boost::bind( &my_class::read_handler, ptr, 
         boost::asio::placeholders::error
         boost::asio::placeholders::bytes_transferred ) );
    

    Often, since the instance is going to be managed via a shared_ptr, which may or may not be in the context of this, it is a good idea to use Boost.SmartPointer’s enable_shared_from_this. When a class inherits from boost::enable_shared_from_this, it provides a shared_from_this() member function that returns a valid shared_ptr instance to this.

    class my_class: public boost::enable_shared_from_this< my_class >
    {
      void read()
      {
         boost::asio::async_read( stream, buffer, 
           boost::bind( &my_class::read_handler, shared_from_this(), 
              boost::asio::placeholders::error
              boost::asio::placeholders::bytes_transferred ) );
      }
    };
    
    boost::shared_ptr< my_class > foo( new my_class() );
    foo->read();
    

    In this snippet, foo.get() and foo->shared_from_this() both point to the same instance. This helps prevent difficult to locate memory leaks. For example, in the original example code, a memory leak occurs in Protocol::AsyncReadMessage if AsyncReadHandler‘s copy-constructor throws when trying to invoke AsyncReadMessage. Boost.Asio’s asynchronous TCP daytime server and many of the examples show enable_shared_from_this being used within Boost.Asio. For a deeper understanding, this question specifically covers asynchronous Boost.Asio functions and shared_ptr.


    Also, in the original code, it may be easier to make Protocol::AsyncHelper a template class instead of having it be a non-templated class with template member functions. This would allow the AsyncHelper to accept the protocol, stream, and handler as constructor arguments, storing them as member variables. Additionally, it makes the bind calls slightly easier to read since it reduces the amount of arguments needing to be passed and since the member functions are no longer templates, there is no need to specify their full type. Here is a quick pass at an example.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.