【发布时间】:2018-12-04 23:00:04
【问题描述】:
伙计们,我在实现具有 map<int,list<Arestas*>>. 的类的析构函数时遇到问题
我的地图将如下所示:
key list
[1] - [2,10] -> [3,100] -> [4,25]
[2] - [1,10] -> [3,50]
[3] - [2,50] -> [1,100] -> [4,40]
[4] - [1,25] -> [3,40]
我的 Arestas 课程包含:
class Arestas {
private:
Fronteira *vertice;
unsigned int custo;
}
我的析构函数现在看起来像这样:
for (auto it = myGrafo.begin(); it != myGrafo.end(); ++it) {
for (auto it1 = (*it).second.begin(); it1 != (*it).second.end(); ++it1) {
delete *it1;
}
(*it).second.clear();
}
但是当我从 key[2] 获取列表时给我这个错误:
_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
#ifdef _DEBUG
_free_dbg(block, _UNKNOWN_BLOCK);
#else
free(block);
#endif
}
提前谢谢你!
编辑
我在我的地图中插入Arestas*,如下所示:
Arestas *aux = new Arestas();
aux->setCusto(_custo);
aux->setVertice(encontrarFronteira(vertice_destino));
// Se o vertice nao existir
if (aux->getVertice()->getVertice() == NULL) {
cout << "ERROR" << endl;
exit(1);
}
myGrafo[vertice_origem].push_back(aux);
// Put the same path in the opposite vertice
Arestas *aux1 = new Arestas();
// set cost
aux1->setCusto(_custo);
// it looks for the vertice in the list<vertices*>
aux1->setVertice(encontrarFronteira(vertice_origem));
myGrafo[vertice_destino].push_back(aux1);
【问题讨论】:
-
需要更多数据。发生了一些不好的事情,但它可能发生在很久以前,程序实际上只是在析构函数期间才意识到它。例如,Rule of Three 违规会导致双重删除。问题:您有充分的理由将
Arestas*存储在列表中吗?消除指针应该会使破坏问题变得毫无意义。 -
是@AdamZahran,感谢大家,我已经解决了这个问题!谢谢!
-
@user4581301 感谢所有提示,从现在开始我会更加关注三法则!
-
查看评论 2。它还要求提供 MCVE,并建议您删除图像。请注意,没有一个答案真正直接回答了您的问题。他们主要建议您查看的地方,因为问题无法按照书面形式正确回答。
-
我们不会对你投反对票,至少我希望人们不会。我们正在否决这个问题。好的(完整的和原创的)问题通常会得到支持。边缘问题通常会被搁置,不好的问题会被否决。看起来这个有一个反对票和两个近距离投票。这还不算太糟糕,真的。有些下降到 -10 并在一分钟内关闭并删除,但这些通常是 Gimmie teh coedz!!! 问题或每天重复多次的问题,例如,为什么@987654332 @ 不等于 3?。你提供了足够好的指导猜测并得到了答案。这是一场胜利。
标签: c++ dictionary pointers memory memory-management