Segfault调用boost :: ip :: tcp :: resolver :: query

时间:2010-11-12 14:25:08

标签: c++ boost-asio

希望我提供了有用的信息,谢谢。

回溯

(gdb) run
Starting program: D:\C++\fail/ircserver.exe
[New thread 4968.0x2f40]
[New thread 4968.0x2cdc]

Program received signal SIGSEGV, Segmentation fault.
Irc::Client::getIP (this=0xbaadf00d)
    at D:/Dev-Cpp/include/boost/smart_ptr/shared_ptr.hpp:409
409             BOOST_ASSERT(px != 0);
(gdb) bt
#0  Irc::Client::getIP (this=0xbaadf00d)
    at D:/Dev-Cpp/include/boost/smart_ptr/shared_ptr.hpp:409
#1  0x004063c9 in Irc::Client::getHostName (this=0xbaadf00d)
    at D:/Dev-Cpp/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bit
s/basic_string.h:1456
#2  0x0041658d in Irc::User::setHost (this=0x3d40a0) at src/user.cpp:320
#3  0x0041136a in Irc::User::User (this=0x3d40a0, _name=@0x3d406c,
    _client=0x3d3ee0) at src/user.cpp:21
#4  0x0040c96d in main (argc=1, argv=0x3d2568) at src/main.cpp:47
(gdb)

的src / user.cpp:320

// ...
void Irc::User::setHost()
{
    if (!client)
        return;

    if (!client->getHostName().empty())  // line 320
        host = client->getHostName();
    else
        host = client->getIP();

    //TODO
}

/src/user.cpp:21

Irc::User::User(const std::string& _name, Client* _client)
    : name(_name), client(client)
{
    joinTime = time(NULL);
    type = UT_NONE;
    idle = 0;
    setHost();  // line 21
    ident = name;
    IP = client->getIP();
    addEvent(1, onIdle);
}

main.cpp中:47

user = new Irc::User(splited[1], it->second.get());

client.cpp:的gethostname

std::string Irc::Client::getHostName()
{
    boost::asio::ip::tcp::resolver::query query(getIP(), SERVER_PORT_STRING); //this line
    boost::asio::ip::tcp::resolver resolver(*m_service);
    boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
    boost::asio::ip::tcp::resolver::iterator end;
    boost::system::error_code error = boost::asio::error::host_not_found;
    while (error && iterator != end)
        *iterator++;

    if (error)
        return std::string();

    return "Found It";
}

client.cpp:getIP

boost::asio::ip::tcp::endpoint remote_ep = sock->remote_endpoint();
boost::asio::ip::address remote_ad = remote_ep.address();
return remote_ad.to_string();

1 个答案:

答案 0 :(得分:2)

0x004063c9 in Irc::Client::getHostName (this=0xbaadf00d)
at D:/Dev-Cpp/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bit

在Windows上,this=0xbaadf00d表示*this未正确初始化。在这种情况下,我怀疑这是因为client(client)定义中的行Irc::User::User,而应该读取client(_client)