"Backtrace:" error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • toutnom
    New Member
    • May 2007
    • 1

    "Backtrace:" error

    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(ch ar * 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_per sonality_v0+0x2 c1)[0x80488e1]
    ./a.out(__gxx_per sonality_v0+0x1 df)[0x80487ff]
    /lib/libc.so.6(__lib c_start_main+0x dc)[0xb7caa87c]
    ./a.out(__gxx_per sonality_v0+0x6 1)[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
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by toutnom
    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(ch ar * 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_per sonality_v0+0x2 c1)[0x80488e1]
    ./a.out(__gxx_per sonality_v0+0x1 df)[0x80487ff]
    /lib/libc.so.6(__lib c_start_main+0x dc)[0xb7caa87c]
    ./a.out(__gxx_per sonality_v0+0x6 1)[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

    Comment

    Working...