473,396 Members | 1,865 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,396 software developers and data experts.

Destructor being called twice ?

Hi ,

Have the following piece of code ,

class ClientList {
public:
char * hostName ;
int portNo ;
struct timeval * tv ;
ClientList ()
{
this -> hostName = new char [40] ;
....
}
~ClientList ()
{
delete [] (this -> hostName) ;
}
};

While running , everything works fine and the destructor is called. But
it seems like the compiler tries to free this memory again on its own ,
giving the following error on g++
*** glibc detected *** double free or corruption (fasttop): 0x0804d008
***

is there something wrong which is being done here ?
thanks in advance .

Mar 24 '06 #1
2 4939
* vivekian:
Hi ,

Have the following piece of code ,

class ClientList {
public:
char * hostName ;
int portNo ;
struct timeval * tv ;
ClientList ()
{
this -> hostName = new char [40] ;
....
}
~ClientList ()
{
delete [] (this -> hostName) ;
}
};

While running , everything works fine and the destructor is called. But
it seems like the compiler tries to free this memory again on its own ,
giving the following error on g++
*** glibc detected *** double free or corruption (fasttop): 0x0804d008
***

is there something wrong which is being done here ?


Yes.

The main problem is that you're using low-level pointers when you should
be using e.g. std::string.

As a consequence, things happen that you didn't think of, here automatic
copy construction and assignment. You can fix those two concrete bugs
by defining a copy constructor and assignment operator. But you cannot
fix the general problem of things occuring that you didn't think of,
except by using safer standard library classes like std::string, where
the authors /did/ think of most untoward things that can occur.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 24 '06 #2
vivekian wrote:
[example of class with pointer to resource snipped]
is there something wrong which is being done here ?


Yes. Look up "the rule of three." If a class has a resource
that is pointed at by a member variable, it almost certainly
wants to have dtor, copy ctor, and assignment operator.
The default copy ctor does a shallow copy. So, for example,
if you call a function that takes an instance of your class,
it makes a copy. And the copy will point at the same chunk
of memory. When the copy is deleted it will delete that
same chunk of memory, producing exactly your problem.
Socks

Mar 24 '06 #3

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

Similar topics

20
by: Agent Mulder | last post by:
Hi group, I have a class called container with a vector of pointers to some base class as member. Can someone help me find the right destructor for this class? Thanks, Martijn Mulder
6
by: Alexander Stippler | last post by:
Hi, I have some question about the usage of delete. I have some reference counted classes, all derived from SharedObject. It's destructor looks like this: ~SharedObject() { if...
52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
9
by: sahukar praveen | last post by:
Hello, This is the program that I am trying. The program executes but does not give me a desired output. ********************************************** #include <iostream.h> #include...
20
by: frs | last post by:
For memory economization, I need to get rid if the virtual destructor. Under this constraint I get caught up in a design, where I need to call a destructor of a derived class from a base class....
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
2
by: Kuku | last post by:
#include<iostream> using namespace std; class Mammal { private: int age, weight;
11
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class...
4
by: David | last post by:
Hi all, something wrong with my code, it seems that the destructor function has been called twice. I don't know why.here is my codes Fixture.h #ifndef _FIXTURE_H #define _FIXTURE_H #include...
12
by: mc | last post by:
Hi Experts, This may be obvious but I can't find anything one way or another. We have a class which is always allocated using new, e.g. Foo* foo = new Foo() so we came up with the idea of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.