You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++体素引擎世界移动算法异常问题排查求助

体素引擎世界移动算法的内存异常问题

我正在用C++开发一款体素引擎,实现了仅沿+x/+y方向移动世界的算法,步骤如下:

  • 分配用于存储旧Chunk指针的缓冲区
  • 将当前Chunk指针复制到缓冲区,并清空当前Chunk指针缓冲区
  • 遍历旧Chunk缓冲区中的每个Chunk,检查其坐标是否位于移动后世界的边界框内
  • 若在边界内,将指针按相对索引复制到当前Chunk缓冲区
  • 否则,删除该Chunk的数据
  • 最后,用坐标正确的新Chunk填充当前Chunk缓冲区中的所有空白(NULL)位置
  • 删除已分配的旧Chunk指针缓冲区

我在外用另一台电脑编写该算法时,它能按预期工作,但回到家用主电脑运行时,却出现错误结果。经进一步测试确认,move函数内调用的所有功能均正常,问题出在move函数本身,我怀疑是某种内存损坏。

以下是move函数代码:

void Level::move(i64 x, i64 y) {
    Chunk **old = new Chunk*[this->view_distance * this->view_distance];

    // some of the edge chunks might need to be updated
    std::vector<Chunk*> mesh_update_chunks = this->get_edge_chunks();

    this->corner_x = x;
    this->corner_y = y;

    // copy all chunk pointers into old
    for (u32 i = 0; i < this->view_distance; i++) {
        std::memcpy(
            (char*)old + i * sizeof(Chunk*) * this->view_distance,
            this->loaded_chunks[i],
            sizeof(Chunk*) * this->view_distance
        );
        std::memset(
            this->loaded_chunks[i],
            0,
            sizeof(Chunk*) * this->view_distance
        );
    }

    // check if a chunk remains in bounds, placing it at the correct index into
    // the chunk array if it does
    for (u32 i = 0; i < this->view_distance; i++) {
        for (u32 j = 0; j < this->view_distance; j++) {
            Chunk *c = old[j + i * this->view_distance];
            if (c->x >= this->corner_x && c->y >= this->corner_y &&
                c->x <=
                    (this->corner_x + this->view_distance * CHUNK_SIZE_XZ) &&
                c->y <=
                    (this->corner_y + this->view_distance * CHUNK_SIZE_XZ)) {
                // chunk remains in bounds
                u32 index_x = (c->x - this->corner_x) / CHUNK_SIZE_XZ;
                u32 index_y = (c->y - this->corner_y) / CHUNK_SIZE_XZ;

                this->loaded_chunks[index_x][index_y] = c;
                continue;
            }

            // if the chunk that is being deleted is in our mesh_update_chunks
            // vector, then we need to erase it
            mesh_update_chunks.erase(
                std::remove(
                    mesh_update_chunks.begin(),
                    mesh_update_chunks.end(),
                    c
                ), mesh_update_chunks.end()
            );

            // delete all data allocated to the chunk
            c->destroy();
            delete c;
        }
    }

    // fill in all of the deleted chunks with new chunks at the correct
    // coordinates
    for (u32 i = 0; i < this->view_distance; i++) {
        for (u32 j = 0; j < this->view_distance; j++) {
            if (this->loaded_chunks[i][j] == NULL) {
                this->loaded_chunks[i][j] = new Chunk;
                this->loaded_chunks[i][j]->x =
                    i * CHUNK_SIZE_XZ + this->corner_x;
                this->loaded_chunks[i][j]->y =
                    j * CHUNK_SIZE_XZ + this->corner_y;

                this->level_generator.run(this->loaded_chunks[i][j]);
                mesh_update_chunks.push_back(this->loaded_chunks[i][j]);
            }
        }
    }

    // mesh all of the chunks that require it
    for (const auto &c : mesh_update_chunks) {
        c->mesh();
    }

    delete[] old;
}

我尚未找到任何bug,制作跟踪表也未发现代码存在问题,请问有人知道错误可能出在哪里吗?

内容的提问来源于stack exchange,提问作者Rajesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:42:03