【发布时间】:2015-05-13 11:30:13
【问题描述】:
我在使用 std::vector 时遇到随机异常。
这里是主要元素:
struct Stroke
{
std::vector<vec2> points;
Pixel color;
std::vector<unsigned int> pixelIds;
};
void myFunc()
{
while (strokes.size() < 5000)
{
if (/*A condition that is always met at some point*/)
{
break;
}
//Some code
newStroke->pixelIds.clear();
newStroke->pixelIds.resize(0);
strokes.push_back(newStroke);
}
for (int i = 0; i < strokes.size(); ++i)
{
drawStroke(strokes[i]);
}
}
void drawStroke(Stroke * currentStroke)
{
std::vector<int> roundIds;
//Fill roundIds
//Some loops and conditions
for (int i = 0; i < roundIds.size(); ++i)
{
if (/*Check condition*/)
{
// Exception is raised deeper in the stack here
currentStroke->pixelIds.push_back(currentRoundId);
}
}
}
我遗漏了不应该真正影响这一点的大部分代码,因为我不知道问题可能来自哪里(所以我不得不复制/粘贴整个代码:D)。在最后一行中,我在堆栈中随机得到一个访问冲突(std::vector 的_Orphan_Range 方法)。
我没有看到手表中的 currentStroke 有什么问题,点向量看起来很正常,颜色也是,我猜 pixelIds 的一些内部值被破坏了(_Myfirst=0x000000000038c700 _MyEnd=0x000000000038c74c 看起来不太好我,但我不确定)。
我对 STL 的细节不是很有经验,我不知道要寻找什么,尤其是因为向量只保存无符号整数值,而不是一些花哨的怪异类或结构,我不知道什么可以搞乱向量的内部值。
任何建议或意见将不胜感激! 谢谢!
【问题讨论】:
-
这个
while(true)部分会杀死一切 -
标准调试技巧:去掉不应该影响问题的部分,然后运行剩下的代码看看是否还有问题。如果你没有问题,你就打破了你的误解。如果您仍有问题,请移除您认为可能有问题的部分,直到您不再有问题为止。当问题消失后,您就会非常清楚问题出在哪里。
-
是什么让您认为异常是“随机的”?承担一些个人责任!
-
"
if (/*A condition that is always met at some point*/)" 不够好。我们怎么知道你做对了?特别是当我们已经知道你有问题时:P 发布一个 testcase。 stackoverflow.com/help/mcve -
不,它不能。您可以始终创建 sscce。这可能需要你一段时间,但你总是可以做到的。事实上,在创建 sscce 时,您很可能会发现并解决您的问题。