简体   繁体   English

C ++数组“矩阵”交换(内存泄漏)

[英]C++ array 'matrix' swap (memory leak)

I'm creating a block based engine and I was working on infinite loading. 我正在创建一个基于块的引擎,并且正在从事无限加载。 I was testing some code, but soon I noticed some troubles. 我正在测试一些代码,但是很快我发现了一些麻烦。 At first I was using an std::unordered_map , storing xz as key and ChunkContainer* as value. 最初,我使用std::unordered_map ,将xz存储为键,将ChunkContainer *存储为值。 I store this as pointers (with new) because it's such a big object it can't be all stored on the stack. 我将其存储为指针(带有new指针),因为它是一个很大的对象,不能全部存储在堆栈中。 It's size is: CHUNK_SIZE(32)^3 * WORLD_HEIGHT(8 amount of chunks in height) * 4(block bytes) = 1048576 bytes . 它的大小是: CHUNK_SIZE(32)^3 * WORLD_HEIGHT(8 amount of chunks in height) * 4(block bytes) = 1048576 bytes (And that * 225 makes up my world.) (那* 225构成了我的世界。)

Then I swapped to using a big array instead of the std::unordered_map so I could achieve faster read speed. 然后,我换成了使用大数组而不是std::unordered_map这样我可以实现更快的读取速度。 So I needed to swap the loading code. 所以我需要交换加载代码。 I came up with this code, 我想出了这段代码,

but I'm having some issues with a memory leak created when using this code: 但是使用此代码时,我遇到了一些内存泄漏问题:

for(int z = 0; z < size; z++){
    temp = loadedChunkContainers[(size-1)*size + z]; //Store the last container in a temp var

    for(int x = size-1; x > 0; x--){
        loadedChunkContainers[x * size + z] = loadedChunkContainers[(x-1) * size + z]; //Move all containers 1 to the right
    }
    int cx = temp.getX() - size;
    int cz = temp.getZ();

    temp.move(cx, cz);//Move the container internally
    loadedChunkContainers[z] = temp; //put the container back into the array, but this time on the first row
    buildQueue.push_back(&loadedChunkContainers[z]);
}

temp is a global variable because I can't store it locally because it will overflow the stack. temp是一个全局变量,因为我不能在本地存储它,因为它将溢出堆栈。 I also can't use swap as it will also overflow the stack. 我也不能使用swap,因为它也会溢出堆栈。

Should I even use this code? 我是否应该使用此代码? It works, but It's quite a slow way in the first place. 它可以工作,但是首先这是一个很慢的方法。 Would there be another way to have the fastest read access while still being able to swap values (without memory leaks)? 是否会有另一种方式来拥有最快的读取访问权限,同时仍然能够交换值(没有内存泄漏)?

An std::unordered_map will never store its elements "on the stack", so this is not a good reason to store pointers. std::unordered_map永远不会将其元素“存储在堆栈上”,因此这不是存储指针的好理由。 You can basically forget all of your stack-related worries, here. 您基本上可以在这里忘记所有与堆栈相关的担忧。 With that goes away all your pointer troubles. 这样就消除了您所有指针的麻烦。 Just store the elements directly and be done with it! 只需直接存储元素并完成操作即可!

If you have measured the std::unordered_map version of your code and found that the (very fast) hash lookup is prohibitively slow in your application, by all means stick with your sparse array, but make it a std::vector so that elements are dynamically allocated [for you]. 如果您测量了代码的std::unordered_map版本,并且发现(非常快的)哈希查找在应用程序中的速度过慢,那么一定要坚持使用稀疏数组,但是将其std::vector以便元素[为您]动态分配。 Then everything I said in the first paragraph still applies. 然后,我在第一段中所说的一切仍然适用。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM