mercredi 19 juin 2019

Boost strands post causing segmentation faults when accessing/inserting values

I'm currently writing a networking structure that merely enacts a server to listen on it's own strand, simple. Whenever the strand is called to call a function and that function calls any members from within the class, segmentation faults occur.

There are a few exceptions but notably these segmentation faults occur when

  1. tcp::acceptor myAcceptor(my_io_service, myEndpoint)
  2. mySockets.insert(...)

Now I could easily swap myAcceptor and place it within the initializer list within the constructor and treat it as a class member but I would like to avoid that.

So whats next to validate my claim? My latest attempt was checking the size of the mySockets which, I should note, is a map. I checked the size prior to calling myStrand.post(...) and checked the size within the function call. Here is what I noticed.

The mySockets map sizes:

  1. Prior to post: 0 (Nothings been inserted, great!)
  2. Within function call, called by post: 999929272 (Ughhhh)

It appears something went airy but what exactly? Did it create a new instance but oddly wouldn't this be 0? It couldn't have lost the address as this would surely cause a segmentation fault, wouldn't it?

This is where I'm at right now in my debugging process.

Goal

The final goal is to be able to have access to mySockets and my_io_service within the post strand.

Layout flow

This explains the layout, the good ol code is below

  1. Create 'work' by passing io_service
  2. Start the io_service by invoking .run()
  3. Create the TCP server
  4. Assign parameters to the class member types
  5. Invoke myStrand.post(...) to call the servers, listen for connecting clients
  6. Wait for client to connect, etc

I've selected code samples that detail the problem.

Header file

using boost::asio::ip::tcp;
bool amIConnected = false;
boost::asio::io_service::strand myStrand;
boost::asio::io_service& my_io_service;
std::map<std::string, std::shared_ptr<tcp::socket>> mySockets;
tcp::acceptor myAcceptor

Source file

ConstructorStuff::ConstructorStuff(boost::asio::io_service& the_service) : 
myStrand(the_service), 
my_io_service(the_service),
myAcceptor(the_service, tcp::endpoint(tcp::v4(), 1337))
{}

void ConstructorStuff::CreateTheServer()
{
    amIConnected = true;
    // std::cout << mySockets.size() << std::endl; Returns 0
    mStrand.post(std::bind(&ConstructorStuff::ListenForConnection, this));
}

void ConstructorStuff::ListenForConection()
{
    // std::cout << mySockets.size() << std::endl; Crazy size
    tcp::acceptor tryAgain(my_io_service, tcp::endpoint(tcp::v4(), 1338)); // Just to showcase the error doesn't just occur through mySockets.
}

Any suggestions would be great!

Side notes:

  • Any additional values like say myPort placed in the header file, initialized to 1337 in CreateTheServer() and then called in ListenForConnection() will abruptly be different.
  • To reassure anyone that may ask, these values may be accessed OUTSIDE the strand, thus locking down this problem to a strand related issue that I'm having.

Aucun commentaire:

Enregistrer un commentaire