Results 1 to 7 of 7

Thread: Segmentation error at a simple return this->property ?

  1. #1
    Join Date
    Feb 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Segmentation error at a simple return this->property ?

    Hey guys,
    my program quits with a segmentation fault.

    The gnu debugger tells me its these lines:

    Qt Code:
    1. TorpiaDownload * download = this->downloads.at (processID);
    2. QString sPath = download->path ();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QString TorpiaDownload::path ()
    2. {
    3. return this->sPath;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef TORPIADOWNLOAD_H
    2. #define TORPIADOWNLOAD_H
    3. #include <QString>
    4. class TorpiaDownload
    5. {
    6. public:
    7.  
    8. QString path ();
    9. int id ();
    10.  
    11. void setPath (QString path);
    12. void setProcess (int ID);
    13. private:
    14. QString sPath;
    15. int processID;
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 

    edit: after reading a bit..
    Qt Code:
    1. int HTTPObject::downloadFile ( QString paramPath)
    2. {
    3. int processID = this->downloads.size ();
    4.  
    5. if (processID >= this->maxRequests)
    6. {
    7. return -1;
    8. }
    9. TorpiaDownload currentDownload;
    10. currentDownload.setPath (paramPath);
    11. currentDownload.setProcess (processID);
    12.  
    13. this->downloads.append (&currentDownload);
    14. this->downloadQueue.append (processID);
    15. if (this->timer->isActive () == false) {
    16. timer->start (this->iInterval);
    17. }
    18. return processID;
    19. }
    To copy to clipboard, switch view to plain text mode 



    now, what i'm trying to do here isn't that sophisticated.

    The TorpiaDownload object holds a path and an id. Now, i want to ask for those two little things. I don't want 'you' to be able to edit them; no, you must use setPath for that.

    Basically, i wanted QString path () to be of type const. But, that didn't work. So i removed it, and i still got that segmentation error .

    Anybody sees my mistake?

    Ps: i'm not used to working with the const keyword. any examples on how to return these (IE, have a normal property and a function that returns it, but casted to const), would be very useful

    thanks in advance
    Last edited by Daimonie; 17th February 2009 at 23:24.

  2. #2
    Join Date
    Feb 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Segmentation error at a simple return this->property ?

    After reading up a bit, i found out what a segmentation error is.
    I made the TorpiaDownloader::sPath public, which fixed the problem a bit.

    I still need your help on how i return a const value properly.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Segmentation error at a simple return this->property ?

    I'm pretty sure this line is incorrect:
    Qt Code:
    1. this->downloads.append (&currentDownload);
    To copy to clipboard, switch view to plain text mode 
    You are storing a pointer to an object that is created on stack and will be deleted once you leave the scope. I'd say the pointer returned by the first line of the first snippet of code you pasted is invalid. Based on how your TorpiaDownload object looks like I suggest to store the object itself and not a pointer to it in the list. All your problems should go away.

  4. #4
    Join Date
    Feb 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Segmentation error at a simple return this->property ?

    Quote Originally Posted by wysota View Post
    I'm pretty sure this line is incorrect:
    Qt Code:
    1. this->downloads.append (&currentDownload);
    To copy to clipboard, switch view to plain text mode 
    You are storing a pointer to an object that is created on stack and will be deleted once you leave the scope. I'd say the pointer returned by the first line of the first snippet of code you pasted is invalid. Based on how your TorpiaDownload object looks like I suggest to store the object itself and not a pointer to it in the list. All your problems should go away.
    Yup, you're right .That's the thing i figured after editing my post and reading up more about Segmentation arrows.

    Now, could by any chance somebody show me how to properly return a constant?
    Qt Code:
    1. #ifndef TORPIADOWNLOAD_H
    2. #define TORPIADOWNLOAD_H
    3. #include <QString>
    4. class TorpiaDownload
    5. {
    6. public:
    7.  
    8. QString path () const;
    9. int id () const;
    10.  
    11. void setPath (QString path);
    12. void setProcess (int ID);
    13. private:
    14. QString sPath;
    15. int processID;
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 
    Now, i *think* this is the right way
    Qt Code:
    1. int TorpiaDownload::id () const {
    2. return this->processID;
    3. }
    To copy to clipboard, switch view to plain text mode 

    But it gave me errors, if i recall correctly. Am i wrong, or is this not the right way and
    how should i do it?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Segmentation error at a simple return this->property ?

    What exactly is the problem? What error message do you get? The code above looks fine.

    By the way, I don't see how making a variable public can make a segmentation fault go away. And I don't consider it related to what I said previously.

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation error at a simple return this->property ?

    Qt Code:
    1. int TorpiaDownload::id () const {
    2. return this->processID;
    3. }
    To copy to clipboard, switch view to plain text mode 

    although it seems all correct, can u just try using "return processID", instead of "return this->processID" .. i believe 'this' pointer would be used by compiler anyway, but just to play around....

  7. #7
    Join Date
    Feb 2009
    Location
    France
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation error at a simple return this->property ?

    Hello,

    I also think that "this>" are nearly never usefule.
    I also agree that const has nothing to do with the segfaults. It can just make easier to understand and thus debug your program.

    Concerning the path QString member, you should always give a reference or a pointer to the setter and take a reference from the getter. As you did, you use copies of the object each time.

    You can then use const references:

    Qt Code:
    1. const QString& path () const {return sPath;}
    2. void setPath (const QString& path) {sPath = path}
    To copy to clipboard, switch view to plain text mode 

    Gaël

Similar Threads

  1. error with QList with a class
    By john_god in forum Newbie
    Replies: 7
    Last Post: 12th January 2009, 21:48
  2. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25

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.