当构造函数退出时,指针将失去引用
Pointers lose reference when constructor exits
下面是一个构造函数,用于形成一个统一的立方体网格(cells
),并将每个cell
的邻居存储在一个向量中。
问题是,当我在构造函数的末尾放置断点时,neighbors
向量中的所有引用都指向具有正确初始化值 ( b
= 5) 的有效单元格。但是,当构造函数退出时,neighbors
向量中的所有内容都变得未初始化,并指向我认为什么都没有(b
= 负无穷大)。但是,cell
本身(保存neighbors
向量)仍然使用正确的值正确初始化。
当构造函数退出时,neighbors
向量的大小也保持正确。由于某种原因,它似乎丢失了对单元格的引用。
CellGrid 头文件(为简洁起见,排除了方法)
class CellGrid {
public:
CellGrid();
CellGrid(int width, int height, int depth);
~CellGrid();
int w;
int h;
int d;
std::vector<std::vector<std::vector<Cell>>> cells;
};
CellGrid 构造函数部分从这里开始
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
for (int k = 0; k < d; k++) {
cells[i][j][k] = Cell();
cells[i][j][k].neighbors.reserve(27);
cells[i][j][k].particles.reserve(8);
}
}
}
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
for (int k = 0; k < d; k++) {
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
for (int z = -1; z < 2; z++) {
if (i + x >= 0 && i + x < w && j + y >= 0 && j + y < h && k + z >= 0 && k + z < d) {
cells[i][j][k].addNeighbor(cells[i + x][j + y][k + z]);
}
}
}
}
}
}
}
细胞结构:
struct Cell {
std::vector<Particle*> particles;
std::vector<Cell*> neighbors;
int b = 5;
void addParticle(Particle &p) {
particles.push_back(&p);
}
void addNeighbor(Cell &c) {
neighbors.push_back(&c);
}
};
了你的项目一段时间以找到问题所在。 尽管该问题暴露为指针因向量更改而失效,但它已被移除一步。 该代码会导致问题,因为您要在 ParticleSystem
构造函数的末尾复制整个CellGrid
。 复制时,将使用所有单元格创建一个新向量,但它们的相邻指针引用从中复制的CellGrid
。 违规代码:
ParticleSystem::ParticleSystem(float deltaT)
{
this->deltaT = deltaT;
// ...a bunch of for loops...
grid = CellGrid((int)width, (int)height, (int)depth);
}
相反,您应该做的是在初始化列表中构造grid
ParticleSystem::ParticleSystem(float deltaT)
: deltaT{deltaT},
grid(width, height, depth)
{
this->deltaT = deltaT;
// ...a bunch of for loops...
}
此外,您应该从CellGrid
中删除复制构造函数和赋值运算符,以便此问题不会在其他地方蔓延
class CellGrid {
public:
CellGrid();
CellGrid(int width, int height, int depth);
CellGrid(const CellGrid&) = delete;
CellGrid& operator=(const CellGrid&) = delete;
//...
};
这将防止问题首先出现。
另一个注意事项是,您的CellGrid
值构造函数比调整向量大小所需的更复杂
CellGrid::CellGrid(int width, int height, int depth)
: w{width},
h{height},
d{depth},
cells(width, std::vector<std::vector<Cell>>(height, std::vector<Cell>(depth, Cell())))
{
for (auto&& row : cells) {
for (auto&& col : row ) {
for (auto&& cell : col) {
cell.neighbors.reserve(27);
cell.particles.reserve(8);
}
}
}
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
for (int k = 0; k < d; k++) {
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
for (int z = -1; z < 2; z++) {
if (i + x >= 0 && i + x < w && j + y >= 0 && j + y < h && k + z >= 0 && k + z < d) {
cells[i][j][k].addNeighbor(cells[i + x][j + y][k + z]);
}
}
}
}
}
}
}
}
由于宽度、高度和深度可以从向量维度推断出来,我还建议去掉变量并添加width()
、height()
和depth()
成员函数来公开维度。
CellGrid
可能不应该是默认可构造的。
除非在每个级别适当地调整cells
大小,否则以下代码块会导致越界内存访问,并导致未定义的行为。
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
for (int k = 0; k < d; k++) {
cells[i][j][k] = Cell();
cells[i][j][k].neighbors.reserve(27);
cells[i][j][k].particles.reserve(8);
}
}
}
您需要的是:
cells.resize(w);
for (int i = 0; i < w; i++) {
cells[i].resize(h);
for (int j = 0; j < h; j++) {
cells[i][k].resize(d);
for (int k = 0; k < d; k++) {
// cells[i][j][k] = Cell(); <-- not needed; done by resize()
cells[i][j][k].neighbors.reserve(27);
cells[i][j][k].particles.reserve(8);
}
}
}
看起来cells
是在CellGrid
的构造函数中声明的,是这样吗?
std::vector<std::vector<std::vector<Cell>>> cells;
这是有问题的,因为cells
按值保存所有Cell
类。当cells
存在作用域时(当构造函数返回时发生),cells
变量被破坏,导致其向量(和嵌套向量,从而Cell
结构)的内容也被破坏。因此,指向Cell
对象的指针是无效的,取消引用它们会导致未定义的行为。
相反,使cells
成为 CellGrid
的私有成员变量,这样做将使cells
的作用域(以及单个结构Cell
结构)保持活动状态,直到CellGrid
本身被破坏。
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 启动类函数作为失去引用的线程
- 为什么对引用的常量引用会失去其恒定性?
- 右值引用失去其性质
- 当构造函数退出时,指针将失去引用
- 右值引用 — 我正在失去值