By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

"Backtrace:" error

P: 1
I can successfully compile the code give below. But at run time, I get some rrors (also given below). I have no clue, why I am getting these errors.

Can somebody help. Thanks in advance.

CODE :
//New.cpp

#include <iostream>

using namespace std;

class Rainbow
{
char* name_;

public:
Rainbow()
{
cout << "Rainbow()" << endl;
name_=NULL;
}
Rainbow(char* name)
{
cout << "Rainbow(char * name)" << endl;
name_=name;
}
~Rainbow()
{
cout << "~Rainbow()" << endl;
delete name_;
}
};


int main()
{
char* name2="RAINBOW 2";

cout << "Rainbow r1" << endl;
Rainbow r1;

cout << "Rainbow r2(name2)" << endl;
Rainbow r2(name2);
}


OUTPUT:
Rainbow r1
Rainbow()
Rainbow r2(name2)
Rainbow(char * name)
~Rainbow()
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x080489da ***
======= Backtrace: =========
/lib/libc.so.6[0xb7cf8911]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7eb9e51]
./a.out(__gxx_personality_v0+0x2c1)[0x80488e1]
./a.out(__gxx_personality_v0+0x1df)[0x80487ff]
/lib/libc.so.6(__libc_start_main+0xdc)[0xb7caa87c]
./a.out(__gxx_personality_v0+0x61)[0x8048681]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:08 413039 /home/varun/programming/cpp/error_programs/a.out
08049000-0804a000 rw-p 00000000 08:08 413039 /home/varun/programming/cpp/error_programs/a.out
0804a000-0806b000 rw-p 0804a000 00:00 0 [heap]
b7c94000-b7c95000 rw-p b7c94000 00:00 0
b7c95000-b7dae000 r-xp 00000000 08:07 14550 /lib/libc-2.4.so
b7dae000-b7db0000 r--p 00118000 08:07 14550 /lib/libc-2.4.so
b7db0000-b7db2000 rw-p 0011a000 08:07 14550 /lib/libc-2.4.so
b7db2000-b7db6000 rw-p b7db2000 00:00 0
b7db6000-b7dc0000 r-xp 00000000 08:07 26182 /lib/libgcc_s.so.1
b7dc0000-b7dc1000 rw-p 00009000 08:07 26182 /lib/libgcc_s.so.1
b7dc1000-b7de4000 r-xp 00000000 08:07 14558 /lib/libm-2.4.so
b7de4000-b7de6000 rw-p 00022000 08:07 14558 /lib/libm-2.4.so
b7e07000-b7e08000 rw-p b7e07000 00:00 0
b7e08000-b7edd000 r-xp 00000000 08:07 26830 /usr/lib/libstdc++.so.6.0.8
b7edd000-b7ee0000 r--p 000d5000 08:07 26830 /usr/lib/libstdc++.so.6.0.8
b7ee0000-b7ee2000 rw-p 000d8000 08:07 26830 /usr/lib/libstdc++.so.6.0.8
b7ee2000-b7ee9000 rw-p b7ee2000 00:00 0
b7ee9000-b7f03000 r-xp 00000000 08:07 14543 /lib/ld-2.4.so
b7f03000-b7f05000 rw-p 00019000 08:07 14543 /lib/ld-2.4.so
bfb74000-bfb8b000 rw-p bfb74000 00:00 0 [stack]
ffffe000-fffff000 ---p 00000000 00:00 0 [vdso]
Abort
May 29 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 181
I can successfully compile the code give below. But at run time, I get some rrors (also given below). I have no clue, why I am getting these errors.

Can somebody help. Thanks in advance.

CODE :
//New.cpp

#include <iostream>

using namespace std;

class Rainbow
{
char* name_;

public:
Rainbow()
{
cout << "Rainbow()" << endl;
name_=NULL;
}
Rainbow(char* name)
{
cout << "Rainbow(char * name)" << endl;
name_=name;
}
~Rainbow()
{
cout << "~Rainbow()" << endl;
delete name_;
}
};


int main()
{
char* name2="RAINBOW 2";

cout << "Rainbow r1" << endl;
Rainbow r1;

cout << "Rainbow r2(name2)" << endl;
Rainbow r2(name2);
}


OUTPUT:
Rainbow r1
Rainbow()
Rainbow r2(name2)
Rainbow(char * name)
~Rainbow()
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x080489da ***
======= Backtrace: =========
/lib/libc.so.6[0xb7cf8911]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7eb9e51]
./a.out(__gxx_personality_v0+0x2c1)[0x80488e1]
./a.out(__gxx_personality_v0+0x1df)[0x80487ff]
/lib/libc.so.6(__libc_start_main+0xdc)[0xb7caa87c]
./a.out(__gxx_personality_v0+0x61)[0x8048681]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:08 413039 /home/varun/programming/cpp/error_programs/a.out
08049000-0804a000 rw-p 00000000 08:08 413039 /home/varun/programming/cpp/error_programs/a.out
0804a000-0806b000 rw-p 0804a000 00:00 0 [heap]
b7c94000-b7c95000 rw-p b7c94000 00:00 0
b7c95000-b7dae000 r-xp 00000000 08:07 14550 /lib/libc-2.4.so
b7dae000-b7db0000 r--p 00118000 08:07 14550 /lib/libc-2.4.so
b7db0000-b7db2000 rw-p 0011a000 08:07 14550 /lib/libc-2.4.so
b7db2000-b7db6000 rw-p b7db2000 00:00 0
b7db6000-b7dc0000 r-xp 00000000 08:07 26182 /lib/libgcc_s.so.1
b7dc0000-b7dc1000 rw-p 00009000 08:07 26182 /lib/libgcc_s.so.1
b7dc1000-b7de4000 r-xp 00000000 08:07 14558 /lib/libm-2.4.so
b7de4000-b7de6000 rw-p 00022000 08:07 14558 /lib/libm-2.4.so
b7e07000-b7e08000 rw-p b7e07000 00:00 0
b7e08000-b7edd000 r-xp 00000000 08:07 26830 /usr/lib/libstdc++.so.6.0.8
b7edd000-b7ee0000 r--p 000d5000 08:07 26830 /usr/lib/libstdc++.so.6.0.8
b7ee0000-b7ee2000 rw-p 000d8000 08:07 26830 /usr/lib/libstdc++.so.6.0.8
b7ee2000-b7ee9000 rw-p b7ee2000 00:00 0
b7ee9000-b7f03000 r-xp 00000000 08:07 14543 /lib/ld-2.4.so
b7f03000-b7f05000 rw-p 00019000 08:07 14543 /lib/ld-2.4.so
bfb74000-bfb8b000 rw-p bfb74000 00:00 0 [stack]
ffffe000-fffff000 ---p 00000000 00:00 0 [vdso]
Abort
name2 is string constant. usually stored in read only section. when you try to delete this memory in destructor of Rainbow it crashes and gives you back trace. try to allocate memory using new then free it

char* name2;
name2 = new char [10];
strcpy (name2 , "Works");

error 2:

the default constructor initializes name_ to Null but u are not checking for NULL before deleting it
May 29 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.