Results 1 to 9 of 9

Thread: Segmentation Fault

  1. #1
    Join Date
    Jul 2007
    Posts
    57
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Segmentation Fault

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

  2. #2
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault

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

  3. #3
    Join Date
    Jul 2007
    Posts
    27
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Segmentation Fault

    Please, show your code.
    My code:
    Qt Code:
    1. DialogImpl::DialogImpl( QWidget * parent, Qt::WFlags f)
    2. : QDialog(parent, f)
    3. {
    4. setupUi(this);
    5. lbl = new QLabel("test", this);
    6. }
    7. //
    8. DialogImpl::~DialogImpl()
    9. {
    10. if (lbl != NULL)
    11. delete lbl;
    12. lbl = NULL;
    13. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jul 2007
    Posts
    57
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault

    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

  5. #5
    Join Date
    Jul 2007
    Posts
    8
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Segmentation Fault

    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

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault

    Quote Originally Posted by comlink21 View Post
    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

  7. #7
    Join Date
    Jul 2007
    Posts
    57
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault

    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

  8. #8
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation Fault

    Quote Originally Posted by Krish_ng View Post
    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 for complete details.

    Therefore, IMHO this would suffice
    Qt Code:
    1. delete m_pLabel;
    2. m_pLabel = NULL;
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Segmentation Fault

    Quote Originally Posted by Krish_ng View Post
    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.
    Qt Code:
    1. // QLabel *m_pLabel=new QLabel; // wrong
    2. m_pLabel=new QLabel(this); // correct
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. Process aborted. Segmentation fault
    By Pragya in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2007, 08:12
  2. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 16:35
  3. segmentation fault insert QString in QCombobox
    By regix in forum Qt Programming
    Replies: 16
    Last Post: 8th August 2006, 08:46
  4. why does qt program meet segmentation fault?
    By wquanw in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 15th May 2006, 16:52
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 16:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.