为什么VS2013在销毁唯一指针时会抛出异常
why does VS2013 throw an exception when destroying unique pointer?
您能否深入了解此异常的含义以及为什么仅在unique_ptr 是 != nullptr 时才抛出它?
代码编译并运行引发异常。
唯一指针 pFace2 在被销毁时似乎会引发异常。
当它 == nullptr 时,它不会引发异常。
VS2013异常信息为:
在网络.exe 0x00CA6A0C的首次机会例外:0xC0000005: 访问违规写入位置0xCCCCCCD0。
如果存在此异常的处理程序,则程序可能是安全的 继续。
代码是:
for (auto volume : domain) {
std::cout << "Volume " << volume->getID() << "n";
for (auto face : volume->volumeFaces) {
auto pNeighbourVolume = std::find_if(
domain.begin(), domain.end(), [&](std::shared_ptr<controlVolume> i) {
return i->getID() == face.getNeighbour();
});
if (pNeighbourVolume != domain.end()) {
std::cout << " connected to " << (*pNeighbourVolume)->getID() << "n";
//This pointer
std::unique_ptr<cvVolumeFace> pFace2 = (*pNeighbourVolume)->matchingFace(face);
std::cout << "n";
} //<- here is where code breaks
}
std::cout << "nn";
}
匹配类型的定义是:
std::unique_ptr<cvVolumeFace> controlVolume::matchingFace(cvVolumeFace &neighboursFace) {
for (auto face : volumeFaces) {
if ((face.getNeighbour() == neighboursFace.getNeighbour()) &&
(face.getArea() - neighboursFace.getArea() < face.matchTolerence())) {
std::cout << "Matched faces for " << face.getNeighbour() << " and " << neighboursFace.getNeighbour();
std::unique_ptr<cvVolumeFace> pFace(&face);
return pFace;
}
}
std::cout << "ERROR: No matching face to return!n";
return nullptr;
};
中断发生在内存中.h 第 116 行
void _Decref()
{ // decrement use count
if (_MT_DECR(_Mtx, _Uses) == 0) //<-breaks here
{ // destroy managed resource, decrement weak reference count
_Destroy();
_Decwref();
}
}
请记住
,pFace2
在if (pNeighbourVolume != domain.end()) { ... }
块结束时被销毁,因此它将尝试删除其资源;在这种情况下,该资源似乎是本地对象。
智能指针不是在设计中不考虑对象生存期的借口。 std::unique_ptr
是控制另一个对象拥有或有限范围内的对象的生存期的好方法。通常,不应传递此类unique_ptr
- 最好仅在该unique_ptr
所有者的生命周期内维护所有权,并将原始指针传递给其他用户,这些用户永远不应该尝试获得所有权。另一方面,如果对象的各种访问器的生存期不确定,则可能应考虑std::shared_ptr
。这是一个更强大的抽象,支持弱引用等,但它也相当昂贵,因为它涉及一个单独分配的控制块和几个原子引用计数器。
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- 函数如何通知用户它基于函数原型抛出异常?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- VisualStudios 会抛出异常,而代码块不会 [C++]
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- ZMQ::send() 抛出异常并终止 QNX 进程.为什么以及如何从中恢复?
- new(std::nothrow) int[n] 抛出异常
- 为什么g_object_set抛出异常(vcruntime140.dll)?
- 你能防止 std::regex 在无效表达式上抛出异常吗?
- C++子线程抛出异常
- 我们应该在抛出异常之前取消分配内存吗
- 有没有一种方法可以让OpenCLC++绑定为所有错误抛出异常
- 为什么访问模板化变体作为返回值抛出异常
- 如何实现,错误分配中止而不是抛出异常
- std::p riority_queue::p op什么时候会抛出异常
- 在0x5914F3BE抛出异常(基于.dll)
- 抛出异常后,对象内分配的指针是否自动解除分配
- 为什么VS2013在销毁唯一指针时会抛出异常
- 由分配器类型提供的非平凡指针的构造抛出异常
- 为什么在抛出异常指针时要通过引用使用 catch