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 993519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:26:10+00:00 2026-05-16T06:26:10+00:00

I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const

  • 0

I wrote a small tcp client using boost::asio, providing the following function:

typedef boost::function<void(const bs::error_code& errCode, size_t bytesTransferred)> ReadHandler;

void CTcpClient::AsyncRead2(std::vector<char>& data, size_t length, ReadHandler readCompletedCallback)
{
    async_read(m_tcpData->socket, ba::buffer(data, length), readCompletedCallback);
}

My idea is to offer the user of my TcpClient class asynchronous operations, without to worry about thread handling, io_services and so on.

Now the code calling the above function from my unittest class looks like this:

CTestTcpClient header

class CTestTcpClient : public ::testing::Test
{
public:
    CTestTcpClient(void);
    virtual ~CTestTcpClient(void);

    struct ReadCompletedHandler
    {
        ReadCompletedHandler() : m_isCompleted(false){};

        void operator()(const boost::system::error_code& errCode, size_t bytesTransferred)
        {
            m_isCompleted = true; // my breakpoint here, checking this pointer
        };

        bool isCompleted(void)
        {
            return m_isCompleted;
        }

    private:
        bool m_isCompleted;
    };

    ReadCompletedHandler m_readHandler;
};

CTestTcpClient source

TEST_F(CTestTcpClient, testAsynchronousRead_Success)
{
    CTcpClient client(testService);
    // Skipped code to setup echo server, connecting and sending of data
    // Receive echo
    vector<char> receiveBuffer(TESTDATA_SIZE);

    client.AsyncRead2(receiveBuffer, TESTDATA_SIZE, m_readHandler);

    while (!m_readHandler.isCompleted())
    {
        client.Wait(200);
    }
}

Now here is the problem: the while loop will never exit, because the is_completed flag is set in a copy of the m_readHandler. If I set a breakpoint in operator() of my handler, I can check and compare the this pointer. It seems boost::asio is copying my handler, call the operator() there, and return. My original handler is never touched at all.

The boost documentation says that copies will be made of the handler as required. So that seems to be ok, but what can I do to achieve the desired behaviour?

Thanks for any help

  • 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-05-16T06:26:11+00:00Added an answer on May 16, 2026 at 6:26 am

    The typical idiom I’ve used for completion handlers is boost::bind a member function using boost::shared_from_this to ensure the object in question does not go out of scope. This is prevalent in nearly all of the Boost.Asio examples as well.

    If you don’t want to change your design you could try boost::ref, for example:

    client.AsyncRead2(receiveBuffer, TESTDATA_SIZE, boost::ref(m_readHandler) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer for rails apps is just filter_parameter_logging :name_of_input simple usage: class… May 16, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer In drupal, use Scanner module which has exactly that functionality.… May 16, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer Not unless your the one writing the the code for… May 16, 2026 at 9:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For my current project, I need to request XML data over a tcp/ip socket
Helllo, I would like to share small amounts of data (< 1K) between python
I have a problem with an J2ME client app, that sends data to an
I'm trying to profile the startup time of my application, so I wrote a
I wrote a simple Java application that reads and writes journals (encrypted text files)
I want to write a small program in .NET 4.0 that will open a
I have previously been using Chilkat Zip .Net to generate backup files from my
I'm doing a small project including a connection to sqlite. I write to two
As was suggested in an answer to my last question ( How do I
I want it to work on windows servers. It will be a cloud type

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.