Strange problem in C++

Hi.
I created a program that creates a file and does not show anything in console.
Program runs without errors but shows something in the console.
What's the problem?
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
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0886a400 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x73e42)[0xb74d8e42]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb76fe51f]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdaPv+0x1b)[0xb76fe57b]
./a.out[0x8049808]
./a.out[0x8049add]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb747e4d3]
./a.out[0x8049161]
======= Memory map: ========
08048000-0804b000 r-xp 00000000 00:14 8918713    /home/majid/Desktop/a.out
0804b000-0804c000 r--p 00002000 00:14 8918713    /home/majid/Desktop/a.out
0804c000-0804d000 rw-p 00003000 00:14 8918713    /home/majid/Desktop/a.out
08868000-08889000 rw-p 00000000 00:00 0          [heap]
b7463000-b7465000 rw-p 00000000 00:00 0 
b7465000-b7604000 r-xp 00000000 08:07 394143     /lib/i386-linux-gnu/libc-2.15.so
b7604000-b7606000 r--p 0019f000 08:07 394143     /lib/i386-linux-gnu/libc-2.15.so
b7606000-b7607000 rw-p 001a1000 08:07 394143     /lib/i386-linux-gnu/libc-2.15.so
b7607000-b760a000 rw-p 00000000 00:00 0 
b760a000-b7626000 r-xp 00000000 08:07 394164     /lib/i386-linux-gnu/libgcc_s.so.1
b7626000-b7627000 r--p 0001b000 08:07 394164     /lib/i386-linux-gnu/libgcc_s.so.1
b7627000-b7628000 rw-p 0001c000 08:07 394164     /lib/i386-linux-gnu/libgcc_s.so.1
b7628000-b7629000 rw-p 00000000 00:00 0 
b7629000-b7653000 r-xp 00000000 08:07 394175     /lib/i386-linux-gnu/libm-2.15.so
b7653000-b7654000 r--p 00029000 08:07 394175     /lib/i386-linux-gnu/libm-2.15.so
b7654000-b7655000 rw-p 0002a000 08:07 394175     /lib/i386-linux-gnu/libm-2.15.so
b7655000-b772d000 r-xp 00000000 08:07 1840077    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b772d000-b772e000
b772e000-b7732000 r--p 000d8000 08:07 1840077    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7732000-b7733000 rw-p 000dc000 08:07 1840077    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7733000-b773a000 rw-p 00000000 00:00 0 
b7752000-b7755000 rw-p 00000000 00:00 0 
b7755000-b7756000 r-xp 00000000 00:00 0          [vdso]
b7756000-b7776000 r-xp 00000000 08:07 394123     /lib/i386-linux-gnu/ld-2.15.so
b7776000-b7777000 r--p 0001f000 08:07 394123     /lib/i386-linux-gnu/ld-2.15.so
b7777000-b7778000 rw-p 00020000 08:07 394123     /lib/i386-linux-gnu/ld-2.15.so
bf848000-bf869000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

I put a try catch block around the whole main function but no exception is thrown.
Memory is being deallocated more than once. This is typically done by have delete or free called on the same pointer twice.
I have a class in this program that has a char* member:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Test
{
   private:
      char* charPtr;
   public:
   Test(int n)
   {
      charPtr = new char[n];
   }
   ~Test()
   {
      delete[] charPtr;
   }
...
}

Is this a serious problem?
How to solve this?
Yeah, that's a serious problem. You probably free the memory of charPtr in some other method of the Test class. You just need to find out where this is happening and remove the unnecessary call to delete in order to solve the problem.
I double checked the code. Just used delete[] once in whole program and it is in a public virtual destructor.
What other thing may cause deallocation?
majidkamali1370 wrote:
What other thing may cause deallocation?

Shallow copies.

--> assignment operator + copy constructor.

Edit: When in doubt google "rule of three".
Last edited on
oops ;)
Thank you. Problem solved.
Topic archived. No new replies allowed.