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

Related Questions

I have just started to learn twisted and wrote a small tcp server/client using
I wrote two small java programs: a TCP client which sends many lines of
I'm just trying to implement a really really small RTSP Client with Boost Asio.
I'm writing a small (C#) client application that sends data using a TCP/IP connection
I wrote small FTP Client Applet which uploads files to FTP Server using Commons
I wrote two small applications (a client and a server) to test UDP communication
I wrote a small internal web app using (a subset of) pylons . As
I wrote some small apps using .NET 3.5 but now I am stuck with
I wrote a small app that turns out to be using a lot of
The http.get() function inside http.createServer is not responding. I wrote a small snippet to

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.