mardi 28 juin 2016

boost::asio::io_service.post() background thread memory leak


I want to run boost::asio::io_service.run() in a background thread. So when I need it post() func into.

This is main func:

int main(int /*argc*/, char** /*argv*/)
{
    std::string message = "hello";

    logg = new logger_client(filename,ip,13666);
    logg->start();

    while (true)
        logg->add_string(message);

    return 0;
}

And some relevant funcs from logger_client:

std::auto_ptr<boost::asio::io_service::work> work;

logger_client::logger_client(std::string& filename,std::string& ip, uint16_t port) : work(new boost::asio::io_service::work(io_service))
{
}

void logger_client::start()
{
    ios_thread = new boost::thread(boost::bind(&io_service.run,&io_service));
}

void print_nothing()
{
    printf("%sn","lie");
}

void logger_client::add_string(std::string& message)
{
    io_service.post(boost::bind(print_nothing));
    //io_service.post(strand->wrap(boost::bind(&logger_client::add_string_imp,this,message)));
    //io_service.run();
}

When i run this, my program eats 2Gb less than a minute. If i remove endless work and change to this:

void logger_client::add_string(std::string& message)
{
    io_service.post(boost::bind(print_nothing));
    //io_service.post(strand->wrap(boost::bind(&logger_client::add_string_imp,this,message)));
    io_service.run();
}

Program works just fine. But I don't want to invoke async operations on this (main) thread. What am i doing wrong?


Aucun commentaire:

Enregistrer un commentaire