Massive memory leaks leads to "corrupted double-linked list:"

Hello, I recently added some code that creates a dynamically allocated, bi-dimensional array, fills it with relevant information and then pass one value of this 2d array to a function through a reference. Here the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Vec3f** dist_light = new Vec3f* [light_container.size()];

for (int i=0; i<n_triangles; i++) {
      ...
      ....   
      dist_light[i] = new Vec3f [n_triangles];
   }
....
for (unsigned int i=0; i<light_container.size(); i++)
      for (int j=0; j<n_triangles; j++)
         dist_light [i][j] = light_container[i].pos - tri_arr[j].getVert11();
....
....
....
  for (unsigned int i=0; i<light_container.size(); i++) {
    ....
    ....
    for (int j=0; j<n_triangles; j++)
      if (tri_arr[j].generateShadows (....,dist_light[i][j],...))


The program keeps running for a while when then closes it with a message:
*** glibc detected *** ./windowManager: corrupted double-linked list: 0x0000000000f06100 ***
======= Backtrace: =========


If I run top while it's running, the memory consumption goes to the limit, while normally it only consumes a couple megabytes. I am certainly doing something very wrong that leads to a massive memory leak.

I have no intention of creating a double-linked list (and as far as my understanding of the matter goes, that is not what should be happening). Only a dynamic bi-dimentional array since number of lights and vertices is variable, given on a file.

So, any idea of what mess I am doing? Thanks for the help.
The doubly-linked list it's talking about is probably the one the memory allocator uses to keep track of memory. You are presumably accessing (writing?) out-of-bounds at some point.

It looks like you may not be allocating the array correctly. You allocate light_container.size() row pointers, but then you loop for n_triangles iterations to create all of the rows (which are each n_triangles in size). Should that loop be for light_container.size() instead?

You could simplify the allocation of your 2D array.
1
2
3
4
5
6
7
8
9
10
11
// Allocate the row pointers.
Vec3f** dist_light = new Vec3f* [light_container.size()];
// Allocate the data as a single contiguous block.
dist_light[0] = new Vec3f [light_container.size() * n_triangles];
// Init the row pointers
for (int i = 1; i < light_container.size(); i++)
    dist_light[i] = dist_light[i-1] + n_triangles

// Delete it like this:
delete [] dist_light[0];
delete [] dist_light;

Last edited on
Why plain arrays at all? The std::vector would manage its memory with less hassle.
Why plain arrays at all? The std::vector would manage its memory with less hassle.

Performance. Unfortunately the vector performance is not good enough. Way slower unfortunately.

Thanks for the explanation tpb. I changed the code to:

1
2
3
4
5
for (unsigned int i=0; i<light_container.size(); i++) {
      dist_light[i] = new Vec3f [n_triangles];
      for (int j=0; j<n_triangles; j++)
         dist_light [i][j] = light_container[i].pos - tri_arr[j].getVert11();
   }


and the memory leak is gone.

Two questions? Do you believe the performance with your method can be superior to mine? I realize I only need one loop with your method. Unfortunately, there is another for loop between these two (and with a different size):

1
2
3
4
5
6
for (unsigned int i=0; i<light_container.size(); i++) {
    ....
    ....
    for (int j=0; j<n_triangles; j++)
      if (tri_arr[j].generateShadows (....,dist_light[i][j],...))
 


But I am thinking about creating a copy of the function where all of this is in and then to get rid of the for loop in between these.
Topic archived. No new replies allowed.