列表迭代器不可解引用?

发布于 2024-08-29 06:06:25 字数 986 浏览 3 评论 0原文

使用以下代码时,我收到错误“列表迭代器不可取消引用”:(

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

以“Counted_message_reader ...”开头的行是给出问题的行)

请注意,该错误并不总是发生,但似乎是随机发生的(通常)当有大量缓冲数据时)。

_list_of_messages 声明如下:

std::list<Counted_message_reader> _list_of_messages;

在周围的代码中,我们可以执行 pop_frontpush_frontsizeemptyend 检查 _list_of_messages,但不调用 erase

我研究了 STL 文档,没有发现任何明显的问题。上面的代码有问题还是我在某个地方有内存泄漏?

谢谢!赞赏!

I get the error "list iterator not dereferencable" when using the following code:

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)

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:

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.

I've studied the STL documentation and can't see any glaring problems. Is there something wrong with the above code or do I have a memory leak somewhere?

Thanks! Appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奢望 2024-09-05 06:06:25

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

This assertion usually indicates some sort of memory corruption bug, such as when you've been manipulating a list from multiple threads or you've overwritten memory that stores the 'bookkeeping' for the list.

千紇 2024-09-05 06:06:25

你能有一个竞争条件吗?

如果列表为空,那么我预计在尝试取消引用 begin() 时会出现问题,但您检查是否为空。您是否有另一个线程并行添加或删除列表中的项目?

您的代码片段在 VS 2008 上适用于我(假设我将 Counted_message_reader 键入 int)。

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

债姬 2024-09-05 06:06:25

对于列表,当迭代器引用的列表元素被删除时,迭代器将失效。这可能就是发生的情况,但我在您的示例代码中没有看到它。野指针在某处? (不确定,编码过多后我可能会失明)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文