Double free or corruption, double linked list

Hello everyone.
I am writing a program where I am creating arrays with new and pointing to them using pointers.
I know, most of you would suggest me to use vectors here instead. But, I am just learning things now and have kept vectors for a later practice.

I am calling a function that creates an array of size 20 using new and after performing some math, returns a pointer to the array. In order to keep my memory free, I delete the arrays that I no longer need using delete[]. Here is my code outline:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  int *Sptr, *Sdptr;

  while(true)
  {
         // capture an image as Img

         Sdptr = my_func(Img);

        // Perform certain operations on the array to check for if image passes  test for level 1

       if(level1 == 1)
       {
              // capture another image as Img

              Sptr = my_func(Img);
 
              // Perform operations on the array for level2 test
              if(level2 == 1)
              {
                     //post-processing operations

              }
              delete [] Sptr;
       }
        delete [] Sdptr;

        // exit while loop if esc key is pressed

}


This program compiles well. While running, the program runs well until level1 is passed for the first time. For first time inside level1, it is successfully able to clear level2. It then completes the post-processing operations and starts to repeat the while loop as it should. It is now that I get an error saying

*** error in ./my_prog: double-free or corruption (!prev): 0x005c0308 ***

or

*** error in ./my_prog: corruption double-linked list

A more detailed dubugging shows that I get th double free error when I try to delete Sptr for the second time after one complete successful cycle.

I am sorry, I can't display all the code as this is a part of my college project and I do not have enough permission from my instructor.
Last edited on
You could try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while ( true )
{
  int * Sdptr = my_func(Img);
  if (level1 == 1)
  {
    int * Sptr = my_func(Img);
    if ( level2 == 1)
    {
    }
    sdt::cout << "del Sptr 0\n"; // for debug only
    delete [] Sptr;
    sdt::cout << "del Sptr 1\n";
  }
  sdt::cout << "del Sdptr 0\n";
  delete [] Sdptr;
  sdt::cout << "del Sdptr 1\n";
}

You could try std::unique_ptr in place of raw pointer too.
http://www.cplusplus.com/reference/memory/unique_ptr/


Edit: dynamic allocation and deallocation is relatively expensive. You could allocate once and simply reuse the arrays rather than reallocate every time.
Last edited on
Hi keskiverto. I tried your solution. For the first few runs, I didn't get this error. But, now I have started getting these errors again. The two errors occur randomly (one at a time).
I do suspect that the real error is somewhere else. A corruption due to out-of-range dereferencing error, perhaps.


I do understand that you desperately want to learn pointers, but do you realize that pointers are more laborious to master than the std::vector? There are still valid uses for pointers, but not as many as before and they are "advanced topics". Admittedly, the use of only crude and simple building blocks emphasizes thinking.


What does generate the:
error in ./my_prog: double-free or corruption

Is it a debugger?
Honestly speaking, I have already completed writing the program of more than 500 lines. I am, thus, feeling lazy to either edit it massively or write another one unless I have no alternative simpler solution. That is the only reason, I am avoiding vectors in this program.

Regarding the error:
I am actually writing the program in Raspbian with a simple text editor. I do not have any debugging tool (nor do I know any without IDE).
I am simply asking the program to display something after every process. The line at which it stops displaying, is the error point (if I understand it right).
With this strategy, I found out that the program ends at the line where I delete Sptr.

1
2
3
4
5
6
7

cout<<"15\t";

delete [] Sptr;

cout<<"14\t";


The output of the above code is



15        `Error XYZ`



Even more weird issue is that, the error keeps changing everytime.
For eg.

*** error in ./my_prog: double-free or corruption (!prev): 0x005c0308 ***

or

*** error in ./my_prog: corruption double-linked list

or some malloc error saying invalid pointer

or terminates abruptly without any error


This can happen after random number of cycles (1 or 2 or 3 or ...)
gdb

is a command line debugger.
This is gdb output. Please help me understand this.


Program received signal SIGABRT, Aborted.
0x76021f70 in __GI_raise (sig=sig@entry=6)
at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) Quit
(gdb)

It says no file by the name raise.c I am not including any such file.
I get this output when there is munmap chunk error: invalid pointer
Last edited on
Topic archived. No new replies allowed.