473,435 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,435 software developers and data experts.

segmentation fault on delete object (which belongs to some class under a hierarchy)

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():p1(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
Jul 22 '05 #1
4 2142
Joel wrote:
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.

Your code is deleting a different object than was constructed. This is
the cause of your segfault.


code
--------------
#include <hash_map.h>
has_map is not standard but is planned to be.

Anyhow - you should try to use the non deprecated header.

#include <ext/hash_map>
#include <iostream>

// gcc requires using namespace __gnu_cxx for hash containers

using namespace __gnu_cxx;
using namespace std;

typedef union {
long r1;
double r2;
} rank_t;
this is easier to read than this whole typedef nonsense.

union rank_t {
long r1;
double r2;
};


struct eqstr {
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) == 0;
}
};

typedef struct oety {
double p1;
double p2;
oety():p1(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();
Allocating an oeety object and assigning it to a eety pointer. This
means that the pointer that is stored is not the pointer that was
returned by new.
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!!!!!!!!!!!!!!!


Attempting to delete a pointer that was not returned by new ...

2 solutions - use virtual destructors for the base classes or design it
differently.
Jul 22 '05 #2

"Joel" <yb****@gmail.com> wrote in message
news:79**************************@posting.google.c om...
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.

It's a simple enough problem see below.
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;
In C++ we prefer

union rank_t {
long r1;
double r2;
};

You are still programming as if you are writing C. You also might want to
look up 'anonymous unions' in your favourite C++ book.

struct eqstr {
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) == 0;
}
};

typedef struct oety {
double p1;
double p2;
oety():p1(1),p2(1) {}
static bool change(oety* s_entry);
} oentry;
Again

struct oentry {
double p1;
double p2;
oety():p1(1),p2(1) {}
static bool change(oety* s_entry);
};

And having the struct name different from the typedef name is REALLY WIERD
and therefore a bad thing.

typedef struct eety {
rank_t rank;
} eentry;
Ditto. And this is the place where using an anonymous union would simplify
your code a little. Delete rank_t above and write this

struct eentry {

union {
long r1;
double r2;
};
};

Then you don't have to write

rank.r2 = 1;

you can just write

r2 = 1;

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;
}


OK here the real point, there is a rule in C++ that you cannot delete an
object through a pointer to its base class unless that base class has a
virtual destructor. Add a virtual destructor to oentry and eentry and you
will be OK. What this means is that any class from which you are considering
deriving another class (now or in the future) should be given a virtual
destructor.

john

BTW is there nay reason not to use std::strings in the code above. Better
than all those char pointers.

john
Jul 22 '05 #3
Thanks for all the reply. I guess I did not realize that destructors
behave so much differently than default constructors. I kind of thought
that there is some "default" destructor, which is called automatically
in the base classes (like default constructors, when you do not have any
non-default defined). Apparently this is not the case, which makes sense
too since you can not really have any non-default destructors.

The project came from C and gradually is being added things c++. Quite
some things, including some coding habbits, still looks like c. Thanks
for the criticizements too.
Jul 22 '05 #4

"Joel" <yb****@gmail.com> wrote in message
news:ck**********@daisy.noc.ucla.edu...
Thanks for all the reply. I guess I did not realize that destructors
behave so much differently than default constructors. I kind of thought
that there is some "default" destructor, which is called automatically in
the base classes (like default constructors, when you do not have any
non-default defined). Apparently this is not the case, which makes sense
too since you can not really have any non-default destructors.


There is a default destructor, and very useful it is too. But it is not a
virtual destructor. When you delete an object through a pointer to a base
class the base class needs a *virtual* destructor.

john
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
18
by: bumps | last post by:
Guys, I am getting segmentation fault while I am trying to open a file in a small function. int PingSamplesList::saveListElements (char *outputFileString) { PingSampleElement *tmpPtr; int...
3
by: mblome | last post by:
Hi everybody! I came across a very strange problem with stl vectors during developement of a mesh creation program. As the program is quite large I can only post small parts of it. Basically I...
5
by: saratoga | last post by:
Hi, all! I've 'Segmentation fault' in this code. I've found that there are two calls to destructor. TestSTL.h: #include <list> #include <string> #include <iostream> using namespace std;
8
by: ChristophK | last post by:
Hallo, this is my first post and I hope I'm doing well, but if not, please let me know. My problem: First all classes and a main-file (I simplified everything to get to the core of the...
2
by: pragtideep | last post by:
Kindly help me explain the behaviour of defult copy constructor . Why the destructor is freeing the SAME memory twice , though it was allocated just once . #include<iostream> using namespace...
6
by: IanWright | last post by:
I've got a segmentation fault problem and am wondering if anyone can help me. I'll apologise now for the fact that I've about a week experience using C++, but I'll try my best to follow any replies...
10
by: H.S. | last post by:
Hello, I have class in which I am allocating space for a double array in the constructor. I use the double array twice in one of the methods and then delete that array in the class's destructor....
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.