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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:30:25+00:00 2026-06-18T03:30:25+00:00

i am trying to setup a boost test for async I/O for tcpip. my

  • 0

i am trying to setup a boost test for async I/O for tcpip.

my function to send tcp msg:

    int TcpClient::sendMsgToServer(string msg) {
    if (isConnected == true) {
        Logger::debug("Asynch send request for msg: " + msg, __LINE__,
                __FILE__);
        sendSequence++;
        msg.append("\r\n");
        int length = msg.length() + 2; //+2 is for \r\n
        char* buffer = (char*) msg.c_str();
        Logger::debug(buffer, __LINE__, __FILE__);
        m_Socket.async_send(boost::asio::buffer(buffer, length),
                boost::bind(&fingigcc::TcpClient::sendMsgToServerErrorHandler,
                        this, boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));
        std::string sentMsg = "Sent Msg: " + msg;
        Logger::debug(sentMsg, __LINE__, __FILE__);


        return 0;
    } else {
        return -1;
    }

}

    void TcpClient::sendMsgToServerErrorHandler(
            const boost::system::error_code& error, std::size_t bytes_transferred) {
        Logger::debug("Sending complete: ", __LINE__, __FILE__);
        sendResponseSequence++;
        int i = error.value();
        std::stringstream ss;
        ss.clear();
        ss << i;
        Logger::debug("RESULTCODE: " + ss.str(), __LINE__, __FILE__);
        if (i == 0) {
            Logger::debug("RESULT: " + error.message(), __LINE__, __FILE__);

        } else {
            Logger::crit(error.message(), __LINE__, __FILE__);
            m_Socket.close();
            isConnected = false;
            connectionStateChanged(isConnected);
        }

    }

now this function works fine if i just run it in the main thread ( not running doing a boost test)

my boost test function looks like this:

BOOST_AUTO_TEST_CASE( CommunicationCore_sendTcpIpMsg_test ) {

    CommunicationCoreFixture f;
    int compare = f.c->initializeTcpIpConnection(serverAddress, serverPort); // i initialize the connection here. runs fine with any issue
    sleep(2);
    compare = f.c->sendMsgToServer("IDENTIFY console-2");
    BOOST_MESSAGE("Sending returned value : " << compare);
    BOOST_CHECK(compare == 0);
}

and it fails with the following error:

    Entering test case "CommunicationCore_sendTcpIpMsg_test"
unknown location(0): fatal error in "CommunicationCore_sendTcpIpMsg_test": memory access violation at address: 0x01003255: no mapping at fault address
*****************Test is aborted

Is there anything i should be aware of while testing such async functions?

my build information is:

Platform: linux
Compiler: GNU C++ version 4.7.2
STL     : GNU libstdc++ version 20120920
Boost   : 1.49.0

EDIT:

I have tried modifying it in the following manner as well..but still getting the same error:

int TcpClient::sendMsgToServer(string msg) {
    if (isConnected == true) {
        Logger::debug("Asynch send request for msg: " + msg, __LINE__,
                __FILE__);
        sendSequence++;
        msg.append("\r\n");
    char *buffer = new char[msg.size() + 1];
    buffer[msg.size()] = 0;
    memcpy(buffer, msg.c_str(), msg.size());
    Logger::debug(buffer, __LINE__, __FILE__);

    m_Socket.async_send(boost::asio::buffer(buffer, (msg.size() + 1)),
            boost::bind(&fingigcc::TcpClient::sendMsgToServerErrorHandler,
                    this, boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
        std::string sentMsg = "Sent Msg: " + msg;
        Logger::debug(sentMsg, __LINE__, __FILE__);


        return 0;
    } else {
        return -1;
    }

}
  • 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-18T03:30:26+00:00Added an answer on June 18, 2026 at 3:30 am

    m_Socket.async_send(boost::asio::buffer(buffer, length) — but msg object is local, and its c_str() result gets invalid after msg goes out of scope! So you actually pass a temporary buffer to an asynchronous operation. The buffer doesn’t outlive this operation, so segfault is inevitable.

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

Sidebar

Related Questions

I'm trying to setup Boost 1.42 in our system. I need Boost to compile
I'm trying setup a subset of boost and get it properly compiled using bjam,
Trying to setup a test page that access a video hosted on S3 and
I m trying setup a cassandra cluster as a test bed but gave the
Trying to setup a game loop thread for a test animation, I have come
Trying to setup a personal image upload/downloader webpage. I've viewed the msdn article on
Trying to setup a CodeIgniter based project for local development (LAMP stack), and once
Trying to setup some validation on the add to cart button for the dropdown
Trying to setup and get going with the RestKit library for a cocoa project
Im trying to setup my asp.net mvc page like the following image: The header

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.