3

我想从页面中恢复所有链接,在执行此代码时我得到:

Microsoft Visual C++ 调试库

调试断言失败!

程序:C:\Users\Gandalf\Desktop\proxy\Debug\Proxy.exe 文件:C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xstring 行:78

表达式:字符串迭代器不可取消引用

有关您的程序如何导致断言失败的信息,请参阅有关断言的 Visual C++ 文档。

(按重试调试应用程序)

中止 重试 忽略

void Deltacore::Client::get_links() {
boost::smatch matches;
boost::match_flag_type flags = boost::match_default;
boost::regex URL_REGEX("^<a[^>]*(http://[^\"]*)[^>]*>([ 0-9a-zA-Z]+)</a>$");

if(!response.empty()) {

    std::string::const_iterator alfa = this->response.begin();
    std::string::const_iterator omega   = this->response.end();

    while (boost::regex_search(alfa, omega, matches, URL_REGEX))
    {
        std::cout << matches[0];
        //if(std::find(this->Links.begin(), this->Links.end(), matches[0]) != this->Links.end()) {
            this->Links.push_back(matches[0]);
        //}
        alfa = matches[0].second;
    }
}
}

任何想法?

添加了更多代码:

        Deltacore::Client client;
    client.get_url(target);
    client.get_links();

            boost::property_tree::ptree props;
            for(size_t i = 0; i < client.Links.size(); i++)
                props.push_back(std::make_pair(boost::lexical_cast<std::string>(i), client.Links.at(i)));

            std::stringstream ss;
            boost::property_tree::write_json(ss, props, false);

            boost::asio::async_write(socket_,
                boost::asio::buffer(ss.str(), ss.str().length()),
                boost::bind(&session::handle_write, this,
                boost::asio::placeholders::error));

提前致谢

4

2 回答 2

4

问题出在这一行:

boost::asio::buffer(ss.str(), ss.str().length())

str()返回一个临时 std::string对象,因此您实际上是在创建缓冲区后立即使其无效 - 正如我评论的那样,香草 UB。;-]

令牌文档引用

对给定字符串对象调用的任何非常量操作都会使缓冲区无效。

当然,销毁字符串符合非常量操作。

于 2012-07-26T22:40:53.510 回答
1

跳过关于使用正则表达式解析 HTML 的讲座(以及你真的不应该如何......),你的正则表达式看起来不像你想要的那样工作。这是你的:

"^<a[^>]*(http://[^\"]*)[^>]*>([ 0-9a-zA-Z]+)</a>$"

第一个字符类将是贪婪的,会吃掉你的 http 和以下部分。您想添加一个问号以使其不贪心。

"^<a[^>]*?(http://[^\"]*)[^>]*>([ 0-9a-zA-Z]+)</a>$"

这可能与异常有关,也可能无关。

于 2012-07-26T22:43:49.570 回答