Discussion:
segmentation fault while deleting a pointer in a destructor
(too old to reply)
H.S.
2008-08-04 03:15:38 UTC
Permalink
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.

Now, that delete operation is giving me a segmentation fault. If I move
the allocation and deletion of the pointer within the method where the
pointer is being is used, it works okay ... but then another pointer
gives a segmentation fault in the destructor when that pointer is being
deleted. In all, there a bunch of pointers being used in that one
method. Memory for these is being allocated in the constructor and being
deleted in the destructor (it is not a virtual class).

I am trying to find where is the problem but haven't successful so far.
Before I continue, I have a specific question.

Am I correct in understanding that the pointer allocation using new in a
constructor of a class, then the usage of that pointer in a class member
and subsequent deletion of the pointer in the class destructor is
supposed to go okay?

Thanks.
a***@gmail.com
2008-08-04 03:51:32 UTC
Permalink
Post by H.S.
Now, that delete operation is giving me a segmentation fault.
Your best bet is to reproduce the problem in the smallest possible
program and to show us. Without seeing what you're doing, we can only
guess. Here is mine:

You haven't defined the copy constructor and/or the assignment
operator, or defined them incorrectly; so the object gets deleted
twice.
Post by H.S.
Am I correct in understanding that the pointer allocation using new in a
constructor of a class, then the usage of that pointer in a class member
and subsequent deletion of the pointer in the class destructor is
supposed to go okay?
Yes.

Ali
H.S.
2008-08-04 04:10:50 UTC
Permalink
Post by a***@gmail.com
Post by H.S.
Now, that delete operation is giving me a segmentation fault.
Your best bet is to reproduce the problem in the smallest possible
program and to show us. Without seeing what you're doing, we can only
I understand that. But the same program works at other times. So I was
not sure if I would be able to reproduce the problem and thought of
verifying my understanding first.
Post by a***@gmail.com
You haven't defined the copy constructor and/or the assignment
operator, or defined them incorrectly; so the object gets deleted
twice.
The variable in question are all double arrays and not any custom
objects. For example, the ctor has:
m_dpz = new double [m_n];

m_dpz gets used as a variable to hold a vector in a member function.

The dtor has:
delete [] m_dpz;

Similarly there are other variables (similar type and length).
Post by a***@gmail.com
Post by H.S.
Am I correct in understanding that the pointer allocation using new in a
constructor of a class, then the usage of that pointer in a class member
and subsequent deletion of the pointer in the class destructor is
supposed to go okay?
Yes.
hmm .. that confirms what I knew. Consequently, a toy example is not
going to show any problems ... I think. With that out of the way, I can
proceed to look for other problems.


Thanks.
a***@gmail.com
2008-08-04 06:27:16 UTC
Permalink
Post by H.S.
The variable in question are all double arrays and not any custom
m_dpz = new double [m_n];
m_dpz gets used as a variable to hold a vector in a member function.
Why not use std::vector?
Post by H.S.
delete [] m_dpz;
a toy example is not going to show any problems ...
The toy example will show the problem, by definition. I said "to
reproduce the problem in the smallest possible program". That smallest
possible program is your toy example and by definition it shows your
problem. Once you start adding pieces to it to reproduce, you will
find the culprit.

I wrote this program that causes a "Segmentation fault" on my system:

class C
{
double * m_dpz;

public:

C()
:
m_dpz(new double[1])
{
m_dpz[-2] = 42;
}

~C()
{
delete[] m_dpz;
}
};

int main()
{
C c0;
}

Ali
blargg
2008-08-04 07:32:26 UTC
Permalink
In article
[...]
[...]
Or, one that might model the original poster's problem:

class C {
double* p;
public:
C() { p = new double [10]; }
~C() { delete [] p; }
};

int main()
{
C c;
C c2( c );
}
H.S.
2008-08-04 15:15:06 UTC
Permalink
Post by blargg
In article
[...]
[...]
class C {
double* p;
C() { p = new double [10]; }
~C() { delete [] p; }
};
int main()
{
C c;
C c2( c );
}
Thanks, though I feel embarrassed you had to create the example. I
usually create the examples in such situations before posting but here I
wanted to verify something before I thought going down that path.

I have been looking where the problem was and have realized that it is
unlikely to occur in the class where the segmentation fault is being given.

It appears that the culprit might be in the arguments being passed to
the class.

Regards,
->HS
H.S.
2008-08-04 15:19:10 UTC
Permalink
Post by a***@gmail.com
Post by H.S.
The variable in question are all double arrays and not any custom
m_dpz = new double [m_n];
m_dpz gets used as a variable to hold a vector in a member function.
Why not use std::vector?
In this particular case I have to provide a pointer to an external
library function. At all other places in my own code I am using STL
vectors and deques though.

Thanks for your work in creating the example below.
->HS
Richard Herring
2008-08-13 11:18:10 UTC
Permalink
In message <g776le$kc4$***@aioe.org>, H.S. <***@gmail.com>
writes
Post by H.S.
Post by a***@gmail.com
Post by H.S.
The variable in question are all double arrays and not any custom
m_dpz = new double [m_n];
m_dpz gets used as a variable to hold a vector in a member function.
Why not use std::vector?
In this particular case I have to provide a pointer to an external
library function.
So use a vector internally, to get the benefits of its memory
management, and pass &v[0] to the external library.
Post by H.S.
At all other places in my own code I am using STL
vectors and deques though.
Thanks for your work in creating the example below.
->HS
--
Richard Herring
James Kanze
2008-08-04 12:59:10 UTC
Permalink
Post by a***@gmail.com
Post by H.S.
Now, that delete operation is giving me a segmentation fault.
Your best bet is to reproduce the problem in the smallest
possible program and to show us. Without seeing what you're
doing, we can only guess.
Actually, in such cases, his best bet is some memory management
tool, like Purify or valgrind, which will (usually) signal the
error where it actualy occurs, rather than just leaving
corrupted memory to create a segment violation later.
Post by a***@gmail.com
You haven't defined the copy constructor and/or the assignment
operator, or defined them incorrectly; so the object gets
deleted twice.
That's also a good guess:-).

--
James Kanze (GABI Software) email:***@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
H.S.
2008-08-04 15:16:56 UTC
Permalink
Post by James Kanze
Post by a***@gmail.com
Post by H.S.
Now, that delete operation is giving me a segmentation fault.
Your best bet is to reproduce the problem in the smallest
possible program and to show us. Without seeing what you're
doing, we can only guess.
Actually, in such cases, his best bet is some memory management
tool, like Purify or valgrind, which will (usually) signal the
Yup, I agree. I thought of doing so last night but then got caught up in
a different minor problem I discovered. I am going to try valgrind now
and see what it comes up with.
Post by James Kanze
error where it actualy occurs, rather than just leaving
corrupted memory to create a segment violation later.
Post by a***@gmail.com
You haven't defined the copy constructor and/or the assignment
operator, or defined them incorrectly; so the object gets
deleted twice.
That's also a good guess:-).
I checked this and this doesn't appear to be case here.

regards,
->HS
H.S.
2008-08-04 21:39:33 UTC
Permalink
Post by H.S.
Post by James Kanze
Post by a***@gmail.com
Post by H.S.
Now, that delete operation is giving me a segmentation fault.
Your best bet is to reproduce the problem in the smallest
possible program and to show us. Without seeing what you're
doing, we can only guess.
Actually, in such cases, his best bet is some memory management
tool, like Purify or valgrind, which will (usually) signal the
Yup, I agree. I thought of doing so last night but then got caught up in
a different minor problem I discovered. I am going to try valgrind now
and see what it comes up with.
Valgrind pin pointed the problem. There was memory corruption being
caused by the arguments passed to the class in question. The class
itself was performing okay, as I had initially assumed, but wanted to
verify the destructor/constructor behavior.

Thanks everyone.
->HS
Loading...