PDA

View Full Version : Segmentation Fault



Krish_ng
30th July 2007, 06:54
When trying to destroy an object i am getting segmentation fault.I hav created some labels
when destroying the same in the destructor of respective class i am getting segmentation fault.I hav used the following statement in the destructr

if(m_pLabel!=NULL)
delete m_pLabel;
m_pLabel=NULL;

is there any dependency we need to check while performing delete...

aMan
30th July 2007, 07:12
If you set their parent in the constructor (new QLable("Blub", this) ), you don't need and aren't allowed to delete them. Qt deletes them in the destructor of the parent object..

ufo-vl
30th July 2007, 07:19
Please, show your code.
My code:


DialogImpl::DialogImpl( QWidget * parent, Qt::WFlags f)
: QDialog(parent, f)
{
setupUi(this);
lbl = new QLabel("test", this);
}
//
DialogImpl::~DialogImpl()
{
if (lbl != NULL)
delete lbl;
lbl = NULL;
}


working fine.
And when destroying parent widget (in this case Dialog, automatically destroyed childs widgets), may be it's issue your segmentation fault?

Krish_ng
30th July 2007, 07:26
I hav used the following statement for the label creation---QLabel *m_pLabel=new QLabel;
But in the class i am inherting QMainWindow--in cpp file i am using the following statement
QClass::QClass():QMainWindow(0);
i am able to use the same delete statemnts for other widgets..But only for some labels i am not able to do so...Is there any other way to get rid of that

comlink21
30th July 2007, 11:01
I think you try to assign the object after deleting it's object.

try the following:

if(m_pLabel!=NULL)
m_pLabel=NULL;
delete m_pLabel;

--comlink21

marcel
30th July 2007, 11:09
I think you try to assign the object after deleting it's object.

try the following:

if(m_pLabel!=NULL)
m_pLabel=NULL;
delete m_pLabel;

--comlink21

But this makes no sense. You first make it null and then you delete it? Won't work.



I hav used the following statement for the label creation---QLabel *m_pLabel=new QLabel;
But in the class i am inherting QMainWindow--in cpp file i am using the following statement
QClass::QClass():QMainWindow(0);
i am able to use the same delete statemnts for other widgets..But only for some labels i am not able to do so...Is there any other way to get rid of that
As it was suggested earlier in this thread, if you pass a parent when you create your widgets, then that parent will handle disposal.

Since it works for other widgets, then I think that you add that label to a layout. In this case the widget that has that layout takes ownership over the label. So you both try to delete it.
I think that you succeed and then the parent widget tries again, and tries to delete an invalid pointer.

What about posting your code?

Regards

Krish_ng
31st July 2007, 06:53
It was my mistake...i had use the statement in the way you specified..i.e.,
if(m_pLabel!=NULL)
delete m_pLabel;
m_pLabel=NULL.

I hav resloved the problem..I was trying delete the label which was already deleted by the
parent.I had added the label to a splitter and deleted splitter object first and was trying to delete the label obj..So i got segmentation fault...Thanks for ur reply

Gopala Krishna
31st July 2007, 13:24
if(m_pLabel!=NULL)
delete m_pLabel;
m_pLabel=NULL.


Actually you don't need to check for null while deleting since deleting a null pointer is perfectly valid in c++. Its just redundancy to check for null pointer. However the pointer should be set to null once deleted.
Check this faq (http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.8) for complete details.

Therefore, IMHO this would suffice

delete m_pLabel;
m_pLabel = NULL;

jpn
7th August 2007, 10:49
I hav used the following statement for the label creation---QLabel *m_pLabel=new QLabel;
Looks like you're creating a new local variable instead of assigning to the member variable.


// QLabel *m_pLabel=new QLabel; // wrong
m_pLabel=new QLabel(this); // correct