c++ - 调用 boost::ip::tcp::resolver::query 时出现段错误

标签 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";
}

客户端.cpp:获取IP

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

最佳答案

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 未正确初始化。在这种情况下,我怀疑这是因为 Irc::User::User 定义中的 client(client) 行,它应该是 client(_client ) 代替。

关于c++ - 调用 boost::ip::tcp::resolver::query 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4165577/

相关文章:

c++ - 为什么代码在使用 unique_ptr 时崩溃,原始指针工作正常?

c++ - tcp 套接字,无法绑定(bind)到端口 - 正在使用

c++ - 仅具有函数的菱形多重继承

c++ - 如何打印宽字符串?

c++ - 将 thread_pool 与 deadline_timer 组合 - 模板问题(时间)

c++ - Visual Studio 链接器找不到 libboost_system

boost::asio tcp async_read 永远不会返回

c++ - 使用 libav 库打包视频

c++ - boost::asio::serial_port 设置 RTS DTS

c++ - 使用 boost::asio::io_service 作为类成员字段