Using Boost::Asio for http client connection with my RestAPI server, things are good but somehow 1/20 times I hit with heap corruption

  boost, boost-asio, boost-beast, c++, heap-corruption
// Connection establisher class
class CSSLConn: public std::enable_shared_from_this<CSSLConn>
{
   :  
   : 
};

// Class for maintaining the thread pool
class CHttpClient
{
  // vector to hold connction objects
  std::vector <std::shared_ptr<CSSLConn>>   m_sslConnObj{};


// main method call this to create connection
void CHttpClient::Initialize(int nThreads)
{
    for (int x = 0; x < nThreadCount; ++x)
    {
        worker_threads_.create_thread(boost::bind(&CHttpClient::WorkerThread, this));
    }

    // let connection get established
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
}

// creating threads for thread pool
void CHttpClient::WorkerThread()
{
    auto client = std::make_shared<CSSLConn>(ioc, m_ctx, sHost, sPort);
    client->connect(sHost.c_str(), sPort.c_str());
    m_sslConnObj.push_back(client);
    ioc->run();  
}

};

1/20 times I hit with heap corruption while it try to create thread pool, mostly with the second thread. I am suspecting std::vector, because it allocate memory while pushing (but I am not sure is it the real culprit). I am maintaining the vector of connection to disconnect all at end.

sometimes crash happens at "boost::system::error_code background_getaddrinfo" function, but all the values looks good here.

I am not much familiar with boost, if anyone know how to debug it better, how can I see what is going inside Boost will help me a lot.

Source: Windows Questions C++

LEAVE A COMMENT