问题描述
为了防止范围蔓延(在
还有其他关于此错误的问题(很有帮助),但是我无法完全理解如何解决它.看起来 qvector.resize()
正在尝试重新分配并可能使用复制构造函数,然后抛出此错误?我可以手动释放内存而不是使用上述函数,但使用智能指针的整个理想是避免内存泄漏......我开始使用 unique_ptr 来解决大量泄漏.
我使用的是 QtCreator 4.4.0 和 Qt 5.6.2,64 位.
--- 编辑---
如果我用 std::vector
替换 QVector
,即 cVoxel 被创建为:
std::vector <std::vector <std::unique_ptr <Voxel>>>c体素;
然后程序在外部 for 循环中崩溃,在:
cVoxel[i].resize(cols);
调试显示:
调试断言失败!
程序:C:\windows\system32\MSVCP140D.dll文件:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector线路:1234
表达式:向量下标超出范围
有关您的程序如何导致断言的信息失败,请参阅有关断言的 Visual C++ 文档.
我可以通过将 cVoxel 的大小调整为 80 而不是 0 来使代码工作.但看起来 QVector.resize()
和 std::vector.resize()
操作.阅读每个文档的文档,它们似乎完全相同.
unique_ptr
不能被复制(只能移动或移动分配):
类 (unique_ptr
) 满足 MoveConstructible 和MoveAssignable,但不是 CopyConstructible 的要求或 CopyAssignable.
复制构造函数和复制赋值在 unique_ptr
中被删除,因此出现错误.cVoxel[i].resize(cols)
隐式需要复制.您可以使用 QVector>
作为替代.
另一件事:在 cVoxel.resize(0)
越界之后调用 cVoxel[i].resize(cols)
.您可能需要 cVoxel.resize(rows)
.
To prevent scope creep (on a previous Q), I've isolated the above error.
My Voxel class definition:
#ifndef VOXEL_H
#define VOXEL_H
#include <QObject>
#include <QVector>
#include <iostream>
include <memory>
class Voxel : public QObject
{
Q_OBJECT
public:
Voxel();
~Voxel();
};
#endif // VOXEL_H
The main file which triggers the error:
#include <voxel.h>
int main(int argc, char *argv[])
{
QVector < QVector <std::unique_ptr <Voxel> > > cVoxel;
cVoxel.resize(0);
int rows = 80, cols = 80;
for (int i = 0; i < rows; i++)
{
cVoxel[i].resize(cols);
for (int j = 0; j < cols; j++)
{
cVoxel[i][j].reset(new Voxel);
}
}
}
The line ultimately throwing the error is:
cVoxel[i].resize(cols);
The full error trace: (which doesn't say that the error ultimately comes main)
There are other Questions (that are helpful) about this error but I can't fully understand how to resolve it. It would seem that qvector.resize()
is attempting to reallocate and likely using a copy constructor which is then throwing this error? I could manually free the memory instead of using the above function, but the whole ideal of using smart pointers is to avoid memory leaks... I started using unique_ptr to address substantial leaking.
I'm using QtCreator 4.4.0 and Qt 5.6.2, 64-bits.
--- EDIT ---
If I replace QVector
with std::vector
, ie cVoxel is created as:
std::vector < std::vector <std::unique_ptr <Voxel> > > cVoxel;
Then the program crashes in the outer for loop, at:
cVoxel[i].resize(cols);
Debugging reveals that:
I can get the code to work by resizing cVoxel to 80 rather than 0. But it appears there's a subtle difference between how QVector.resize()
and std::vector.resize()
operate. Reading the docs for each, they seemed identical.
unique_ptr
cannot be copied (only moved or move-assigned):
The copy-constructor and copy-assignment are deleted in unique_ptr
hence the error. Copying is implicitly required by cVoxel[i].resize(cols)
.You could use QVector<QVector<Voxel>>
as an alternative.
Another thing: calling cVoxel[i].resize(cols)
after cVoxel.resize(0)
is out-of-bounds. You probably need cVoxel.resize(rows)
.
这篇关于unique_ptr, qvector.resize() 尝试引用已删除的函数时抛出错误 2280的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!