J
Joel
I have this bug that quite puzzled me. Basically I am having a
segmentation fault on deleting an object, which belongs to a class
which is the result of multiple inheritance from two other classes.
None of the classes actually allocate memory on the heap. I simplified
my code into one piece to show below. I will really appreciate if
someone can tell me where the problem is. I have spent quite some time
on it and felt a bit lost at this point.
the compiler is g++ (GCC 3.3.2 on Mandrake)
Thank you for your help!
Joel
code
--------------
#include <hash_map.h>
typedef union {
long r1;
double r2;
} rank_t;
struct eqstr {
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) == 0;
}
};
typedef struct oety {
double p1;
double p2;
oety()1(1),p2(1) {}
static bool change(oety* s_entry);
} oentry;
typedef struct eety {
rank_t rank;
} eentry;
typedef hash_map<char*, eentry*, hash<char*>, eqstr> EType;
typedef struct oeety : public oentry, public eentry {
oeety() {rank.r2 = 1;}
} oeentry;
int main ( int argc, int argv[] ) {
EType* extraInfo = new EType();
eentry* entry1;
entry1 = new oeentry();
char * s_name = new char[2];
s_name[0] = 'a';
s_name[1] = '\0';
(*extraInfo)[s_name] = entry1;
entry1 = new oeentry();
s_name = new char[2];
s_name[0] = 'b';
s_name[1] = '\0';
(*extraInfo)[s_name] = entry1;
EType::iterator itr = extraInfo->begin();
cout << "point 1" << endl;
delete itr->second; //segmentation fault!!!!!!!!!!!!!!!
cout << "point 2" << endl;
return 0;
}
---------------
result:
point 1
Segmentation fault
segmentation fault on deleting an object, which belongs to a class
which is the result of multiple inheritance from two other classes.
None of the classes actually allocate memory on the heap. I simplified
my code into one piece to show below. I will really appreciate if
someone can tell me where the problem is. I have spent quite some time
on it and felt a bit lost at this point.
the compiler is g++ (GCC 3.3.2 on Mandrake)
Thank you for your help!
Joel
code
--------------
#include <hash_map.h>
typedef union {
long r1;
double r2;
} rank_t;
struct eqstr {
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) == 0;
}
};
typedef struct oety {
double p1;
double p2;
oety()1(1),p2(1) {}
static bool change(oety* s_entry);
} oentry;
typedef struct eety {
rank_t rank;
} eentry;
typedef hash_map<char*, eentry*, hash<char*>, eqstr> EType;
typedef struct oeety : public oentry, public eentry {
oeety() {rank.r2 = 1;}
} oeentry;
int main ( int argc, int argv[] ) {
EType* extraInfo = new EType();
eentry* entry1;
entry1 = new oeentry();
char * s_name = new char[2];
s_name[0] = 'a';
s_name[1] = '\0';
(*extraInfo)[s_name] = entry1;
entry1 = new oeentry();
s_name = new char[2];
s_name[0] = 'b';
s_name[1] = '\0';
(*extraInfo)[s_name] = entry1;
EType::iterator itr = extraInfo->begin();
cout << "point 1" << endl;
delete itr->second; //segmentation fault!!!!!!!!!!!!!!!
cout << "point 2" << endl;
return 0;
}
---------------
result:
a.out
point 1
Segmentation fault