C ++迭代器作为lambda函数中的参数,断言失败:“矢量迭代器不兼容”

时间:2017-10-26 13:34:56

标签: c++ visual-studio-2015 stl iterator

在使用iterator作为参数调用lambda函数后,我遇到了一个奇怪的行为。它产生一个断言失败“矢量迭代器不兼容”。

我的代码看起来像这样:

Assertion failed: This should be an update of the last segment in a period
shaka.media.SegmentIndex.merge @ segment_index.js:153
shaka.dash.SegmentTemplate.createStream @ segment_template.js:82
shaka.dash.DashParser.parseRepresentation_ @ dash_parser.js:1080
shaka.dash.DashParser.parseAdaptationSet_ @ dash_parser.js:978
shaka.dash.DashParser.parsePeriod_ @ dash_parser.js:722
shaka.dash.DashParser.parsePeriods_ @ dash_parser.js:626
shaka.dash.DashParser.processManifest_ @ dash_parser.js:520
(anonymous) @ dash_parser.js:431
Promise resolved (async)
shaka.dash.DashParser.parseManifest_ @ dash_parser.js:430
(anonymous) @ dash_parser.js:397
Promise resolved (async)
shaka.dash.DashParser.requestManifest_ @ dash_parser.js:390
shaka.dash.DashParser.onUpdate_ @ dash_parser.js:1161
setTimeout (async)
shaka.dash.DashParser.setUpdateTimer_ @ dash_parser.js:1209
(anonymous) @ dash_parser.js:331
Promise resolved (async)
shaka.dash.DashParser.start @ dash_parser.js:329
(anonymous) @ player.js:555
Promise resolved (async)
shaka.util.CancelableChain.then @ cancelable_chain.js:72
shaka.Player.load @ player.js:499
initPlayer @ ?x=149&y=88&q=http%3A%2F%2Flocalhost%3A8080%2Fmeta%2F%3FbucketID%3D%2FVolumes%2FTV%2FFa%26channelID%3D%2FVolumes%2FTV%26containerID%3D%2FUsers%2F%2Fbrev%%2FL1ZvbHVtZXMvVFYvRmFudGFzdGljLkJlYXN0cy5hbmQuV2hlcmUudG8uRmluZC5UaGVtLjIwMTYuQkRSaXAueDI2NC1TUEFSS1MvYS5ta3Y%2F97M4TS6IqnQ.mpd:392
463segment_index.js:145 Refusing to rewrite original references on update!
shaka.media.SegmentIndex.merge @ segment_index.js:145
shaka.dash.SegmentTemplate.createStream @ segment_template.js:82
shaka.dash.DashParser.parseRepresentation_ @ dash_parser.js:1080
shaka.dash.DashParser.parseAdaptationSet_ @ dash_parser.js:978
shaka.dash.DashParser.parsePeriod_ @ dash_parser.js:722
shaka.dash.DashParser.parsePeriods_ @ dash_parser.js:626
shaka.dash.DashParser.processManifest_ @ dash_parser.js:520
(anonymous) @ dash_parser.js:431
Promise resolved (async)
shaka.dash.DashParser.parseManifest_ @ dash_parser.js:430
(anonymous) @ dash_parser.js:397
Promise resolved (async)
shaka.dash.DashParser.requestManifest_ @ dash_parser.js:390
shaka.dash.DashParser.onUpdate_ @ dash_parser.js:1161
setTimeout (async)
shaka.dash.DashParser.setUpdateTimer_ @ dash_parser.js:1209
(anonymous) @ dash_parser.js:331
Promise resolved (async)
shaka.dash.DashParser.start @ dash_parser.js:329
(anonymous) @ player.js:555
Promise resolved (async)
shaka.util.CancelableChain.then @ cancelable_chain.js:72
shaka.Player.load @ player.js:499
initPlayer @ ?x=149&y=88&q=http%3A%2F%2Flocalhost%3A8080%2Fmeta%2F%3FbucketID%3D%2FVolumes%2FTV%2FFa%26channelID%3D%2FVolumes%2FTV%26containerID%3D%2FUsers%2F%2Fbrev%%2FL1ZvbHVtZXMvVFYvRmFudGFzdGljLkJlYXN0cy5hbmQuV2hlcmUudG8uRmluZC5UaGVtLjIwMTYuQkRSaXAueDI2NC1TUEFSS1MvYS5ta3Y%2F97M4TS6IqnQ.mpd:392

中的vector.h中引发了断言
std::vector<LAYOUT> Layouts;
// fill vector...
auto it = Layouts.begin();
while (it != Layouts.end()) {
    // do something...
    auto fn= [Layouts] (auto i) -> void {
        while (i != Layouts.end()) { // <- ASSERTION RAISED HERE
            // do something...
            i++;
        }
    };
    fn(it);
    it++;
}

问题是为什么会发生这种情况?

1 个答案:

答案 0 :(得分:4)

我花了一些时间在这里找到真正的问题。解决方案很简单:void std::_Vector_const_iterator<_Myvec>::_Compat(const _Myiter& _Right) const { // test for compatible iterator pair if (this->_Getcont() != _Right._Getcont()) { _DEBUG_ERROR("vector iterators incompatible"); _SCL_SECURE_INVALID_ARGUMENT; } } 未在capture子句中作为引用传递。因此,首先生成该向量的副本。然后很清楚地知道Layouts迭代器与lambda函数中作为参数给出的原始向量迭代器不兼容。

解决方案是在capture子句中添加引用运算符(&amp;)。

Layouts.end()