Results 1 to 6 of 6

Thread: function returns QString, usage generates segfault

  1. #1
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default function returns QString, usage generates segfault

    I'm creating a function of the form:
    Qt Code:
    1. QString &DNumber::toString()
    2. {
    3. qDebug() << "DNumber as string";
    4. char * buffer = new char[length()];
    5. mReal->toFixPtString(buffer, mDecimalPlaces);
    6. QString result(buffer);
    7. delete buffer;
    8. return result;
    9. }
    To copy to clipboard, switch view to plain text mode 

    The function toFixPtString(...) is from a library and it has to use char *, and it has to be dynamically allocated so that the length can be different at runtime. I want to return a QString which is not dynamically allocated, so I don't have to find a way to make sure it gets deleted. Is there a particular method for this? If I try to do anything with the value it returns, the program generates a segfault. Additionally, the compiler issues a warning about returning a local variable.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: function returns QString, usage generates segfault

    You are returning a variable which is deleted/freed when the function returns, and thus is not valid by the time the function returns, hence the seg fault.

    You'll need to dynamically allocate the qstring, or return a class variable or local static allocation of it, or pass the QString to be assigned into the function.

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

    space_otter (5th March 2010)

  4. #3
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: function returns QString, usage generates segfault

    There was conveniently a QString class member for me to access, and using that fixed the error. Thanks. When you said "returning a variable which is deleted/freed" I thought you meant the char *, but if Qstring was copying the string like it was supposed and then getting deleted, that wouldn't help. It makes me wonder why compiler issued a warning and not an error.

    Qt Code:
    1. QString &DNumber::toString()
    2. {
    3. qDebug() << "DNumber as string";
    4. char * buffer = new char[length()];
    5. mReal->toFixPtString(buffer, mDecimalPlaces);
    6. mValue = buffer;
    7. delete buffer;
    8. return mValue;
    9. // the code runs and the warning dissapears, mValue persists
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: function returns QString, usage generates segfault

    I would change the definition from
    Qt Code:
    1. #
    2. QString &DNumber::toString()
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. #
    2. QString DNumber::toString()
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Dec 2009
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: function returns QString, usage generates segfault

    I was wondering about that. Some of the API function use const QString& for input and QString& output so I copied that without really understanding the reason. I'll change it.

  7. #6
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: function returns QString, usage generates segfault

    Perhaps it is time to understand the difference...

    (QString &), is a reference to a string. Think of a reference as a pointer that has automatic de-referencing ability. In other words, using (QString&) is very similar to using (QString*).

    With a reference, you can change the value of the item. This makes the reference a good choice for an argument (if you want to change the original copy), or if you want to change the value of a class member variable. The problem that you have, however, is that you returned a reference to a temporary variable that went out of scope when the function returned the value. This means that the reference is to a string that no longer exists.

    If you simply return QString, then a temporary variable is created and used as the return value. The disadvantage is that a temporary QString is created to be used as the return value.

    Define a function as follows:

    Qt Code:
    1. void whatever(const QString& x)
    To copy to clipboard, switch view to plain text mode 

    Now, as a test, ponder what happens with this call

    Qt Code:
    1. whatever("hello");
    To copy to clipboard, switch view to plain text mode 


    http://developer.kde.org/~wheeler/cpp-pitfalls.html
    http://www.learncpp.com/cpp-tutorial...-by-reference/

Similar Threads

  1. call function as Qstring
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2009, 01:35
  2. char* to QString. Segfault after delete []
    By TheRonin in forum Qt Programming
    Replies: 9
    Last Post: 19th June 2008, 13:20
  3. QString memory usage
    By jogeshwarakundi in forum Newbie
    Replies: 1
    Last Post: 13th December 2007, 06:48
  4. Method/Function With QString Arg
    By seanmu13 in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2007, 09:43
  5. Using QString in C function ...
    By Godlike in forum Newbie
    Replies: 10
    Last Post: 8th April 2006, 17:01

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.