c++ - 表达式 : string iterator not dereferencable while using boost regex

标签 c++ regex visual-c++ boost boost-regex

我想从一个页面中恢复所有链接,同时执行我得到的代码:

Microsoft Visual C++ Debug Library

Debug Assertion Failed!

Program: C:\Users\Gandalf\Desktop\proxy\Debug\Proxy.exe File: C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xstring Line: 78

Expression: string iterator not dereferencable

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

Abort Retry Ignore

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));

提前致谢

最佳答案

问题出在这一行:

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

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

代币 documentation citation :

The buffer is invalidated by any non-const operation called on the given string object.

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

关于c++ - 表达式 : string iterator not dereferencable while using boost regex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11678537/

相关文章:

c++ - 在windows上使用zlib编译boost 1.62/1.63

regex - 如何在groovy/gradle中使用正则表达式替换csv中的文本?

opencv - cv::gemm 中断言失败 (a_size.width == len)

c++ - 将 Windows 上的 32 位和 64 位 Boost 构建到同一文件夹中

c++ - 带有可变参数模板的自定义容器 emplace

c++ - PyQt 与 PySide 比较

regex - 在 Sublime Text 2 中展开文本

c# - 正则表达式并排匹配 2 个数字或字符

c++ - 在 Windows 上从 1_55 更新到 1_62 后,创建一个 boost::asio::ip::udp::socket 在 LIB 中崩溃

c++ - Visual c++ "for each"可移植性