简体   繁体   English

列出迭代器不能解除引用?

[英]List iterator not dereferencable?

I get the error "list iterator not dereferencable" when using the following code: 使用以下代码时出现错误“list iterator not dereferencable”:

bool done = false;
while (!_list_of_messages.empty() && !done) {
    // request the next message to create a frame
    // DEBUG ERROR WHEN NEXT LINE IS EXECUTED:
    Counted_message_reader reader = *(_list_of_messages.begin());
    if (reader.has_more_data()) {
        _list_of_frames.push_back(new Dlp_data_frame(reader, _send_compressed_frames));
        done = true;
    } else {
        _list_of_messages.pop_front();
    }
}

(The line beginning with "Counted_message_reader..." is the one giving the problem) (以“Counted_message_reader ...”开头的行是给出问题的那一行)

Note that the error doesn't always occur but seemingly at random times (usually when there's lots of buffered data). 请注意,错误并不总是发生,但似乎是随机时间(通常在有大量缓冲数据时)。

_list_of_messages is declared as follows: _list_of_messages声明如下:

std::list<Counted_message_reader> _list_of_messages;

In the surrounding code we could do pop_front , push_front and size , empty or end checks on _list_of_messages but no erase calls. 在周围的代码我们可以做pop_frontpush_frontsizeemptyend对检查_list_of_messages但没有erase电话。

I've studied the STL documentation and can't see any glaring problems. 我研究过STL文档,看不出任何明显的问题。 Is there something wrong with the above code or do I have a memory leak somewhere? 上面的代码有什么问题,或者我在某处有内存泄漏?

Thanks! 谢谢! Appreciated! 不胜感激!

此断言通常表示某种内存损坏错误,例如,当您从多个线程操作列表或者您已覆盖存储列表的“簿记”的内存时。

Could you have a race-condition? 你有竞争条件吗?

If the list were empty, then I'd expect a problem when trying to dereference begin(), but you check for empty. 如果列表为空,那么在尝试取消引用begin()时会出现问题,但是检查为空。 Do you have another thread adding or removing items from list in parallel? 您是否有另一个线程并行添加或删除列表中的项目?

Your code snippets works for me on VS 2008 (assuming I typedef Counted_message_reader to int ). 您的代码片段在VS 2008上适用于我(假设我将typedef Counted_message_readerint )。

For a list, iterators are invalidated when the element of the list that it refers to is erased. 对于列表,当它引用的列表的元素被擦除时,迭代器将失效。 That's probably what happens, but I don't see it in your sample code. 这可能发生了什么,但我没有在您的示例代码中看到它。 Wild pointer somewhere ? 某处的野指针? (not sure, I may be blind after of too much coding). (不确定,我可能会因为编码过多而失明)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM