segmentation fault, please help!

So I have been writing this program and it was running ok, up until I asked the user they wanted to see any of the objects they've created. After it ran and did everything it was supposed to, i got this gigantic error:

** Error in `./a.out': free(): invalid pointer: 0x00000035744f25d0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x356f277d9e]
/lib64/libc.so.6(cfree+0x5b5)[0x356f2839f5]
./a.out[0x400e52]
/lib64/libc.so.6(__libc_start_main+0xf0)[0x356f21ffe0]
./a.out[0x400af9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 fd:02 2885509 /home/students/FALL15/chynnahernandez/CISC2000/PolygonLab/a.out
00602000-00603000 r--p 00002000 fd:02 2885509 /home/students/FALL15/chynnahernandez/CISC2000/PolygonLab/a.out
00603000-00604000 rw-p 00003000 fd:02 2885509 /home/students/FALL15/chynnahernandez/CISC2000/PolygonLab/a.out
01c6d000-01c8e000 rw-p 00000000 00:00 0 [heap]
356ee00000-356ee21000 r-xp 00000000 fd:00 787172 /usr/lib64/ld-2.20.so
356f021000-356f022000 r--p 00021000 fd:00 787172 /usr/lib64/ld-2.20.so
356f022000-356f023000 rw-p 00022000 fd:00 787172 /usr/lib64/ld-2.20.so
356f023000-356f024000 rw-p 00000000 00:00 0
356f200000-356f3b3000 r-xp 00000000 fd:00 790081 /usr/lib64/libc-2.20.so
356f3b3000-356f5b3000 ---p 001b3000 fd:00 790081 /usr/lib64/libc-2.20.so
356f5b3000-356f5b7000 r--p 001b3000 fd:00 790081 /usr/lib64/libc-2.20.so
356f5b7000-356f5b9000 rw-p 001b7000 fd:00 790081 /usr/lib64/libc-2.20.so
356f5b9000-356f5bd000 rw-p 00000000 00:00 0
3570200000-3570307000 r-xp 00000000 fd:00 793591 /usr/lib64/libm-2.20.so
3570307000-3570506000 ---p 00107000 fd:00 793591 /usr/lib64/libm-2.20.so
3570506000-3570507000 r--p 00106000 fd:00 793591 /usr/lib64/libm-2.20.so
3570507000-3570508000 rw-p 00107000 fd:00 793591 /usr/lib64/libm-2.20.so
3570600000-3570616000 r-xp 00000000 fd:00 792638 /usr/lib64/libgcc_s-4.9.2-20150212.so.1
3570616000-3570815000 ---p 00016000 fd:00 792638 /usr/lib64/libgcc_s-4.9.2-20150212.so.1
3570815000-3570816000 r--p 00015000 fd:00 792638 /usr/lib64/libgcc_s-4.9.2-20150212.so.1
3570816000-3570817000 rw-p 00016000 fd:00 792638 /usr/lib64/libgcc_s-4.9.2-20150212.so.1
3574200000-35742f0000 r-xp 00000000 fd:00 795260 /usr/lib64/libstdc++.so.6.0.20
35742f0000-35744f0000 ---p 000f0000 fd:00 795260 /usr/lib64/libstdc++.so.6.0.20
35744f0000-35744f8000 r--p 000f0000 fd:00 795260 /usr/lib64/libstdc++.so.6.0.20
35744f8000-35744fa000 rw-p 000f8000 fd:00 795260 /usr/lib64/libstdc++.so.6.0.20
35744fa000-357450f000 rw-p 00000000 00:00 0
7f0d046b0000-7f0d046b5000 rw-p 00000000 00:00 0
7f0d046dd000-7f0d046e1000 rw-p 00000000 00:00 0
7fffae096000-7fffae0b7000 rw-p 00000000 00:00 0 [stack]
7fffae1e6000-7fffae1e8000 r--p 00000000 00:00 0 [vvar]
7fffae1e8000-7fffae1ea000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)

why am i getting this core dump? I have never encountered this problem before. How can I fix it if it can be fixed?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include "Polygon.h"

main(){
        const int MAX = Polygon::getMaxPolygons();

        Polygon *pol[MAX]; //create an array of pointer objects

        int N;
        bool validate = false;

        do{
                cout << "Welcome to the Polygon Creator! How many polygons would you like to create today?" << endl;
                cin >> N;
                if (N <= MAX){
                        for ( int i = 0; i < N; i++ ){
                                pol[i] = new Polygon;
                                pol[i] -> display();
                                pol[i] -> input();
                                pol[i] -> display();
                        }

                        cout << "Number of Polygons created is: "<< Polygon::getNumPolygons()<< endl;
                        validate = true;
                }

        }while(!validate);

        bool validChoice = false;
        bool retstat= false;
        char choice;
        int i;

        do{
                cout << "Would you like to see any of the polygons you've created? Enter y for yes, n for no." << endl;
                cin >> choice;

                if (choice == 'Y'|| choice=='y'){
                        do{
                                cout << "Which object would you like to see?" << endl;
                                cin >> i;

                                if ( i <= N){
                                        pol[i-1] -> DrawPolygon();
                                        validate = true;
                                        retstat = true;
                                }

                        }while(!validate);
                }
  }

                else if (choice == 'N' || choice == 'n'){
                        cout << "Ok! Thanks for trying the polygon creator!" << endl;
                        validChoice = true;
                }

        }while(!validChoice);

        delete pol[N];
}


Last edited on
The crash is because you are trying to use (or delete[]) a pointer to memory that does not belong to you. In other words, your pointers are not being managed correctly.

I suspect something is wrong with the way you are managing polygons. You have some kind of Polygon class that is supposed to be able to manage multiple polygons? (It has a getNumPolygons() method that you are using.)

As a wild guess, does a Polygon delete itself when destructed?
Yeah, my program takes manages multiple polygons. What is the proper way to use delete in this case? My destructor looks like this:
Polygon::~Polygon(){
cout << "Destructor runs." <<endl;

numPolygons--;
}

How do I go about fixing this problem?
1
2
             cout << "Welcome to the Polygon Creator! How many polygons would you like to create today?" << endl;
                cin >> N;


N is the number of polygons being allocated with new in pol.

delete pol[N];

So pol[N] was not allocated with new. You are not allowed to delete it.

Something like this is probably what you meant:
1
2
    for (unsigned i=0; i<N; ++i)
        delete pol[i];
Oh ok, but if I do that then wouldn't I be deleting all of the contents of these objects? I would like to use them later when I ask users if they want to see any of the objects they've created.
Oh ok, but if I do that then wouldn't I be deleting all of the contents of these objects?

Yes, you would. But the delete statement doesn't occur until right before the program ends, so I really don't see any issue there.
Thank you so much for that clarification. I appreciate your help.
Topic archived. No new replies allowed.