Results 1 to 8 of 8

Thread: app crashes again

  1. #1
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy app crashes again

    Hi again, I'm faced with the second application's crash.
    Here is my code:
    Qt Code:
    1. void DishManager::initPage1()
    2. {
    3. ui.p1FamilyComboBox->addItems(dishes->getFamilies());
    4. }
    To copy to clipboard, switch view to plain text mode 
    where p1FamilyComboBox is a instance of
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    , and dishes is an instance of the following class Record.

    Qt Code:
    1. const QStringList& Record::getDishNamesByFamily(const QString &family) const
    2. {
    3. QStringList names;
    4. for(int i = 0; i < dishes.size(); i++)
    5. {
    6. if(dishes[i].getFamily() == family)
    7. {
    8. names << dishes[i].getName(); //如果某菜谱属于此菜系,则加入其菜名
    9. }
    10. }
    11. return names;
    12. }
    To copy to clipboard, switch view to plain text mode 
    I think maybe it's due to the
    Qt Code:
    1. const QStringList& Record::getDishNamesByFamily
    To copy to clipboard, switch view to plain text mode 
    Is there anything wrong with the returning value.
    Thanks in advance!!

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: app crashes again

    change

    Qt Code:
    1. const QStringList& Record::getDishNamesByFamily()
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. QStringList Record::getDishNamesByFamily()
    To copy to clipboard, switch view to plain text mode 

    阅读有关悬挂指针(引用)
    Last edited by nish; 15th June 2009 at 07:19.

  3. The following user says thank you to nish for this useful post:

    sophister (15th June 2009)

  4. #3
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: app crashes again

    Oh, thank you!!
    You know Chinese Simplified!!(简体中文).
    But I do not realize it would be something with the reference or pointer. C++ is really strange!

  5. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: app crashes again

    it is very simple...

    the local var
    QStringList names;
    is destroyed when the function returns... so the pointer(reference) to it(the return type) is pointing to the
    illegal memory hence the crash..


    Btw .. i just used google translater to translate.. i dont know chinese no matter how simplified it is...

  6. #5
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: app crashes again

    Oh, I see.
    Thanks very much!!!

  7. #6
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: app crashes again

    The following codes have any problem??
    Qt Code:
    1. const QString& getName() const {return name;}
    To copy to clipboard, switch view to plain text mode 
    it is a method of class Dish. name is a member variable of class Dish.
    When I do the following, it crashes again:
    Qt Code:
    1. label.setText(dish.getName());
    To copy to clipboard, switch view to plain text mode 

    Is this the same problem as above?
    Thanks!!

  8. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: app crashes again

    it should not crash now... can u write a minimal compilable example reproducing the problem?

  9. #8
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: app crashes again

    thanks.
    I have modify my codes.
    I remember that it is said in the <Thinking in C++>, that reference is only to be used in function variables. Maybe I overuserd it.

Similar Threads

  1. Replies: 1
    Last Post: 30th March 2009, 22:25
  2. QGraphicsScene render() crashes
    By iebele in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2008, 13:38
  3. Replies: 10
    Last Post: 19th October 2007, 19:17
  4. Designer crashes when selecting some widgets
    By gwendal in forum Qt Tools
    Replies: 4
    Last Post: 21st July 2006, 13:18

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.