我正在尝试在我的 Qt5 应用程序中使用 libtorrent,但不断收到诸如 malloc() 之类的消息的段错误:内存损坏。经过数小时的调试,我想出了触发此问题的一小段代码:
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    std::string filename = "fedora.torrent";
    libtorrent::error_code ec;
    libtorrent::add_torrent_params parameters;
    std::cerr << "111\n";
    parameters.ti = new libtorrent::torrent_info(filename, ec);;
    std::cerr << "222\n";
    return app.exec()
}
在这种情况下,torrent_info 的构造函数会产生段错误。但是,如果我在创建 QGuiApplication 之前移动 libtorrent 相关代码,如下所示:
int main(int argc, char *argv[])
{
    std::string filename = "fedora.torrent";
    libtorrent::error_code ec;
    libtorrent::add_torrent_params parameters;
    std::cerr << "111\n";
    parameters.ti = new libtorrent::torrent_info(filename, ec);;
    std::cerr << "222\n";
    QGuiApplication app(argc, argv);
    return app.exec()
}
然后它工作得很好。此外,此问题仅存在于 32 位版本中,在 64 位版本中,两种变体的工作方式相同。