c++ - std::string messed up when using it as storage within (boost.)async_read_some -


i using async_read_some read data port saved in char[] called _data. buffer size big enough every request:

void start() {      socket_.async_read_some(boost::asio::buffer(data_,buffersize),make_custom_alloc_handler(allocator_,boost::bind(&attentionsession::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)));  }  void handle_read(const boost::system::error_code& error, size_t bytes_transferred) {      string ip = socket_.remote_endpoint().address().to_string();     log->processdata(data_,ip,"memory");     strcpy(data_,"");  } 

processdata adds additional information (like timestamp etc.) request copying newly alloced char*. char[] sent writetomemory(char*) append char* std::string memorybuffer:

void writecachetofile() {      // function writes buffer data log file      char* temp = new char[memorybuffer.length() + 1];     strcpy(temp, memorybuffer.c_str());     writetofile(temp);     delete[] temp;     memorybuffer.clear();  }  void writetomemory(char* data) {      int maxsize = 1024;       // checks if char* data 'fit' pre-defined maxsize      if ((strlen((const char*)data) + memorybuffer.length()) >= maxsize) {         writecachetofile(); // if not cache memorybuffer saved first     }      memorybuffer.append((const char*) data);      cout << memorybuffer.length() << endl;  } 

it works if there requests (bombarding requests) things messed up. can see above in writetomemory() function i'll added line print out current length of memorybuffer , think has somethings thread safety of std::strings:

96 188 284 3639 94 190 286 2591 102 198 294 388 484 2591 96 2591 96 190 286 2591 

the length of each (processed processdata()) request 96 characters. here length of memorybuffer rises , falls down - lengths bigger maxsize (1024 chars).

edit: sam pointed out should add more code. how start io_service:

boost::asio::io_service ioservice; boost::scoped_ptr<boost::thread> ioservicethread;  server_class server (ioservice,port); // create server instance ioservicethread.reset (new boost::thread ( boost::bind ( &boost::asio::io_service::run, &ioservice  ) ) );  // 1 threaded io_service (to receive user inputs in main() function) 

and async_acceptor's function after completing request:

typedef boost::shared_ptr<session_class> session_ptr;  void handleaccept(session_ptr thissession, const boost::system::error_code& error) {     if (!error) {       thissession->start(); // calls start() function above       thissession.reset(new session(ioservice,logging_class));       acceptor.async_accept(thissession->socket(),boost::bind(&server_class::handleaccept, this, thissession, placeholder_error));     }  } 

the session_class holds functions start() , handle_read(x,y) mentioned above. logging_class provides class write log files (holds functions writecachetofile() , writetomemory(char*)). log (mentioned above) kind of class.

eoe: end of edit

if try fix outsource caching part (appending received char* std::string) boost::threads ends totally mixed memorybuffer

is thread safety of std::strings or else missed?

thanks in advance! :)

paul

i made same point comment figured worth expanding answer. posting snippets of code have not terribly helpful, don't give full picture. example, following concepts not clear me:

  1. you're not checking bytes_transferred parameter in async_read_some handler. very important because though tell read n bytes can return when reading n - x bytes x <= n. documentation states, should consider using 1 of composed operations async_read free function.
  2. you're using custom memory allocations asynchronous read operations, presumably based on example provided. why need that?
  3. buffer lifetime. ex: buffers stay in scope until async_read handler invoked?
  4. object lifetime. ex: using shared_ptr properly? io_service in scope entirety of event loop?
  5. are using single io_service per process or 1 per thread?
  6. why need threads? typically it's easier understand asynchronous programming in single threaded context first.

all of these important concepts right when using boost.asio. part of debugging boiling down perceived problem smaller reproducer. useful both on stack overflow , becomming programmer in general. understand problem, , us find it. suggest spend effort on making smaller reproducible example can compile. if that's not possible, consider using multiple threads after have proved single threaded scenario works.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -