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

segmentation fault while deleting a pointer in a destructor

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.
Aug 4 '08 #1
10 12440
On Aug 3, 8:15 pm, "H.S." <hs.sa...@gmail.comwrote:
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.
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
Aug 4 '08 #2
ac******@gmail.com wrote:
On Aug 3, 8:15 pm, "H.S." <hs.sa...@gmail.comwrote:
>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.

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.
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).
>
>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.

Aug 4 '08 #3
On Aug 3, 9:10 pm, "H.S." <hs.sa...@gmail.comwrote:
acehr...@gmail.com wrote:
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.
Why not use std::vector?
The dtor has:
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
Aug 4 '08 #4
In article
<8c**********************************@b38g2000prf. googlegroups.com>,
ac******@gmail.com wrote:
[...]
I wrote this program that causes a "Segmentation fault" on my system:
[...]
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 );
}
Aug 4 '08 #5
On Aug 4, 5:51 am, acehr...@gmail.com wrote:
On Aug 3, 8:15 pm, "H.S." <hs.sa...@gmail.comwrote:
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.
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.
That's also a good guess:-).

--
James Kanze (GABI Software) email:ja*********@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
Aug 4 '08 #6
blargg wrote:
In article
<8c**********************************@b38g2000prf. googlegroups.com>,
ac******@gmail.com wrote:
>[...]
I wrote this program that causes a "Segmentation fault" on my system:
[...]

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

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
Aug 4 '08 #7
James Kanze wrote:
On Aug 4, 5:51 am, acehr...@gmail.com wrote:
>On Aug 3, 8:15 pm, "H.S." <hs.sa...@gmail.comwrote:
>>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.
error where it actualy occurs, rather than just leaving
corrupted memory to create a segment violation later.
>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.

That's also a good guess:-).

I checked this and this doesn't appear to be case here.

regards,
->HS
Aug 4 '08 #8
ac******@gmail.com wrote:
On Aug 3, 9:10 pm, "H.S." <hs.sa...@gmail.comwrote:
>acehr...@gmail.com wrote:
>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.

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
Aug 4 '08 #9
H.S. wrote:
James Kanze wrote:
>On Aug 4, 5:51 am, acehr...@gmail.com wrote:
>>On Aug 3, 8:15 pm, "H.S." <hs.sa...@gmail.comwrote:
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

Aug 4 '08 #10
In message <g7**********@aioe.org>, H.S. <hs**************@gmail.com>
writes
>ac******@gmail.com wrote:
>On Aug 3, 9:10 pm, "H.S." <hs.sa...@gmail.comwrote:
>>acehr...@gmail.com wrote:
>>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.

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.
>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
Aug 13 '08 #11

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

Similar topics

4
by: Joel | last post by:
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....
4
by: deancoo | last post by:
Ok, I've got another one for ya. The code I've pasted below compiles fine but produces a seg fault. I've put a comment in where the seg fault gets tripped. Can someone please explain what I've...
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...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
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...
9
by: aamircheema | last post by:
Hi, I have written a program (my first big program in c++). When I run the program it gives segmentation fault but when i use a printf statement to debug the program, it runs normally. I am very...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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...

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.