How to catch strange undefined behaviour in C++ code?

你。 提交于 2019-12-01 20:49:13

问题


I have strange behaviour in server program. In simple example it works fine (I insert traces everywhere, in pion and asio).

#include <pion/http/server.hpp>
#include <pion/http/response_writer.hpp>
#include <pion/http/response_reader.hpp>
#include <pion/http/request_writer.hpp>
#include <pion/logger.hpp>
#include <pion/scheduler.hpp>

int main()
{
   pion::single_service_scheduler shed;
   shed.set_num_threads(1);
   boost::shared_ptr<pion::http::server> server
   (new pion::http::server(shed, 5000));
   server->add_resource("/", handlerFunction);
   server->start();
   sleep(5);
}

output is like this. Construct socket for acceptor, construct socket for client, tcp connection is created, all works fine.

basic io object constructor
after service construct
basic io object constructor
after service construct
basic io object constructor
Address of socket is: 0x9855fa4 value: -1
after service construct
1422519945 INFO pion.http.server Added request handler for HTTP resource: 
1422519945 INFO pion.http.server Starting server on port 5000
before connection create
before connection constructor called
basic io object constructor
basic_stream_socket::construct
Address of socket is: 0x9857514 value: -1
after impl.construct
after service construct
basic io object constructor
after service construct
basic io object constructor
after service construct
ssl socket constructed
connection constructor, is_ssl: 0
after connection create: 0x98574f8
before accept
after accept

In more complicated program with same code, but with oracle and many other libraries output is like this.

basic io object constructor
after service construct
basic io object constructor
after service construct
basic io object constructor
Address of socket is: 0xbfe47a64 value: -1
after service construct
1422525476 INFO pion.http.server Added request handler for HTTP resource: 
before connection create
basic io object constructor
after service construct
basic io object constructor
after service construct
after connection create: 0x8fe8b88
before accept
in connection::async_accept
after accept

No second socket created, actually, there is no call of connection::create, but connection has address as you can see. I have idea, that somewhere something writes on address of function connection::create (or something like). Can you please help, how can I catch this?


回答1:


On ubuntu, I like to run with valgrind (http://valgrind.org/).

sudo apt-get install valgrind
valgrind ./mypgrogram

It doesn't report all issues, but when it does, it will report the nature and origin.

Also recommended:

valgrind --db-attach=yes ./myprogram

Which allows you to debug (backtrace, inspect) and continue the program when a violation/uninitialized memory reference is detected.

On some older Ubunti I had to use sudo to make valgrind be able to attach gdb:

sudo -E valgrind --db-attach=yes ./myprogram

If tr1/unordered_map should be quite trivial to replace with std::unordered_map

E.g. with a quick hack

#include <unordered_map>

namespace std { namespace tr1 {

    using std::unordered_map;
    using std::hash;
    // etc...
} }

Of course this is not good practice, and you might just want to typedef between std::unordered_map and std::tr1::unordered_map instead, but in the interest of quick checks...



来源:https://stackoverflow.com/questions/28211746/how-to-catch-strange-undefined-behaviour-in-c-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!