Results 1 to 4 of 4

Thread: Segmentation fault when creating new QString

  1. #1
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Segmentation fault when creating new QString

    I have:

    Qt Code:
    1. //Node.cpp
    2.  
    3. #include "Node.h"
    4.  
    5. Node::Node(QWidget *parent)
    6. : QLabel (parent) {
    7. }
    8.  
    9. void Node::setName(string picName) {
    10. nodePicName = new QString(QString::fromStdString(picName));
    11. }
    To copy to clipboard, switch view to plain text mode 

    and then in my "Node.h" file I have:

    Qt Code:
    1. class Node : public QLabel {
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. Node(QWidget *parent = 0);
    7. virtual ~Node();
    8. void setName(string picName);
    9.  
    10. private:
    11. QString *nodeName;
    12. }:
    To copy to clipboard, switch view to plain text mode 

    I am getting a segmentation fault at where I have:
    Qt Code:
    1. nodePicName = new QString(QString::fromStdString(picName));
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault when creating new QString

    I don't know what exactly goes wrong, but why don't you just use
    Qt Code:
    1. QString nodeName;
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void setName(std::string name)
    2. {
    3. nodeName = QString::fromStdString(name);
    4. }
    To copy to clipboard, switch view to plain text mode 

    or what would have my preference

    Qt Code:
    1. void SomeClass::setName(const QString &name)
    2. {
    3. nodeName = name;
    4. }
    To copy to clipboard, switch view to plain text mode 

    then call the function using

    Qt Code:
    1. obj.setName(QString::fromStdString(somestring));
    To copy to clipboard, switch view to plain text mode 

    There is no need to use pointers for QStrings since they implement implicit sharing, which basically means that if you copy a string, you only copy a reference to the data. When you start to change the contents, then a deep copy is made. This design approach makes it easier for you to code, less error prone (less memory leaks) and more readable.
    Last edited by franz; 28th September 2009 at 21:27. Reason: updated contents
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. The following 2 users say thank you to franz for this useful post:

    di_zou (29th September 2009), George Neil (29th September 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation fault when creating new QString

    So I changed it around to this:

    Qt Code:
    1. void Node::setName(const QString &name)
    2. {
    3. nodeName = name;
    4. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. class Node : public QLabel {
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. Node(QWidget *parent = 0);
    7. virtual ~Node();
    8. void setName(const QString &name);
    9.  
    10. private:
    11. QString nodeName;
    12. };
    To copy to clipboard, switch view to plain text mode 

    but I still get a seg fault inside the setName method.

  5. #4
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation fault when creating new QString

    Nvrmnd got it working. Thank you.

Similar Threads

  1. Replies: 21
    Last Post: 28th September 2010, 10:59
  2. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  3. segmentation fault on closing
    By harakiri in forum Qt Programming
    Replies: 6
    Last Post: 8th July 2009, 13:54
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  5. segmentation fault insert QString in QCombobox
    By regix in forum Qt Programming
    Replies: 16
    Last Post: 8th August 2006, 08:46

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.