【发布时间】:2012-08-23 13:01:45
【问题描述】:
是否有人使用 BWAPI 在访问当前游戏的 Unit 对象时遇到访问冲突错误?
我确定错误不在我的代码中.. 无论如何.. 有什么办法可以避免访问冲突吗?
我有时会在下面的注释中遇到此错误.. 下面的代码执行了很多次,但有时我会收到错误..
int Squad::getSize() {
int no = 0;
for (int i = 0; i < (int) agents.size(); i++) {
BaseAgent* agent = agents.at(i);
if (agent != NULL && agent->isAlive() && agent->getUnit() != NULL && !agent->getUnit()->isBeingConstructed()) // this line
no++;
}
return no;
}
这是我用来从向量中删除 BaseAgent 的代码。分析一下,看看我能不能做得更好:
void AgentManager::cleanup() {
//Step 2. Do the cleanup.
int cnt = 0;
int oldSize = (int)agents.size();
for (int i = 0; i < (int)agents.size(); i++) {
if (!agents.at(i)->isAlive()) {
delete agents.at(i);
agents.erase(agents.begin() + i);
cnt++;
i--;
}
}
int newSize = (int)agents.size();
}
BaseAgent 代码在此link
【问题讨论】:
-
为什么不尝试检查指针是否为 NULL?
-
我已经完成了..不是空指针..
-
人族 imba,只是说
-
您缺少
NULL检查agent->getUnit()。 -
@juanchopanza 我也已经这样做了。我从代码中删除了,因为问题不在于
标签: c++ exception access-violation