Results 1 to 20 of 22

Thread: Segfault when retrieving info from QSettings

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Segfault when retrieving info from QSettings

    Alright, in my application I have a "settings" class, with various public functions which return or set the data requested/specified. However, this line of code returns a segfault:

    Qt Code:
    1. QString settings::getSomeInfo() {
    2. return internalAppSettings->value("main/info", "default").toString();
    3. }
    To copy to clipboard, switch view to plain text mode 

    Note that this file's header is included in my Main Window class, which in turn is included in main.cpp. Also, in main.cpp, I used QCoreApplication::setOrganizationName(), QCoreApplication::setApplicationName(), and QCoreApplication::setOrganizationDomain() so that I don't need to do it many times whenever I have to declare QSettings.

    This is the output from GDB, the debugger:

    Qt Code:
    1. Program received signal SIGSEGV, Segmentation fault.
    2. [Switching to Thread -1225369392 (LWP 17790)]
    3. 0x08063322 in settings::getSomeInfo (this=0x0) at src/settings.cpp:104
    4. Use the -dograb option to enforce grabbing.
    5. Scope for 104:
    6. Symbol this is a variable with complex or multiple locations (DWARF2), length 4.
    7. (gdb)
    8. Program terminated with signal SIGSEGV, Segmentation fault.
    9. The program no longer exists.
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance ~codeslicer

  2. #2
    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: Segfault when retrieving info from QSettings

    You have a null pointer which you are trying to dereference in line 104 of src/settings.cpp. Looks like you didn't initialize your settings object.

  3. #3
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segfault when retrieving info from QSettings

    Initialize? Is this what you mean:


    Qt Code:
    1. settings::settings(QWidget * parent) : QWidget(parent) {
    2. QSettings internalAppSettings(this);
    3. }
    To copy to clipboard, switch view to plain text mode 

    I did this in my constructor.

  4. #4
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Segfault when retrieving info from QSettings

    I declared it in my header file too..
    Qt Code:
    1. private:
    2. //Internal QSettings declaration
    3. QSettings* internalAppSettings;
    To copy to clipboard, switch view to plain text mode 

    Could this be because the value doesn't exist? For example it has never been set before and it's returning null, but in that case wouldn't it return the string "default"? Or like the message said, are there multiple sources?

    Symbol this is a variable with complex or multiple locations (DWARF2), length 4. (gdb)

    I'm stumped

  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: Segfault when retrieving info from QSettings

    Quote Originally Posted by codeslicer View Post
    Qt Code:
    1. settings::settings(QWidget * parent) : QWidget(parent) {
    2. QSettings internalAppSettings(this);
    3. }
    To copy to clipboard, switch view to plain text mode 
    This creates a local variable called internalAppSettings that gets destroyed immediately when the constructor returns. Your member variable is left uninitialized. Try this instead:
    Qt Code:
    1. settings::settings(QWidget *parent) : QWidget(parent){
    2. internalAppSettings = new QSettings(this);
    3. }
    To copy to clipboard, switch view to plain text mode 

    Or better yet simply don't declare "internalAppSettings" as a pointer but instead as a regular object and then leave the constructor empty.

  6. The following user says thank you to wysota for this useful post:

    codeslicer (16th March 2008)

  7. #6
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segfault when retrieving info from QSettings

    Thanks! I didn't know that the variable gets deleted.

  8. #7
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segfault when retrieving info from QSettings

    Sorry for the delay, I thought it would work, but both of the solutions still return segfaults, with different errors...

    Creating a QSettings object for each of the members solves the problem, but I think that's unneccesary. So... any help? I did a google search and noticed this happened in QDevelop to someone...

  9. #8
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Wink Re: Segfault when retrieving info from QSettings

    Just in case, attached is the relevant code:

    main.cpp
    Qt Code:
    1. int main(int argc, char ** argv)
    2. {
    3. ...
    4. QCoreApplication::setOrganizationName("app");
    5. QCoreApplication::setApplicationName("appmakers");
    6. QCoreApplication::setOrganizationDomain("app.com");
    7. ...
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 


    settings.cpp
    Qt Code:
    1. #include <QtCore>
    2. #include "settings.h"
    3.  
    4. settings::settings(QWidget * parent) : QWidget(parent) {
    5. internalAppSettings = new QSettings(this);
    6. }
    7. ...
    8. QString settings::getSomeInfo() {
    9. return internalAppSettings->value("main/info", "default").toString();
    10. }
    To copy to clipboard, switch view to plain text mode 


    settings.h
    Qt Code:
    1. #ifndef SETTINGS_H
    2. #define SETTINGS_H
    3.  
    4. #include <QWidget>
    5. #include <QSettings>
    6.  
    7. class settings : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. settings(QWidget * parent = 0);
    12. ...
    13. QString getSomeInfo();
    14. ...
    15.  
    16. private:
    17. //Internal QSettings declaration
    18. QSettings *internalAppSettings;
    19. };
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    Personally I don't see anything wrong with this... Yet I still get a segfault:

    Qt Code:
    1. Symbol this is a variable with complex or multiple locations (DWARF2), length 4.
    To copy to clipboard, switch view to plain text mode 

    This is really weird Any hints?
    Last edited by codeslicer; 18th March 2008 at 01:10. Reason: spelling error

  10. #9
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segfault when retrieving info from QSettings

    **BUMP**

    Any ideas?

  11. #10
    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: Segfault when retrieving info from QSettings

    Do you need members in those classes? I personally create and destroy the settings objects when I need them.

    Qt Code:
    1. void C::m(p){
    2. QString v = s.value("xxx");
    3. //...
    4. }
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segfault when retrieving info from QSettings

    Eh.. well my application needs to access QSettings pretty often... creating all those QSettings would be a waste of space and a source of confusion...

  13. #12
    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: Segfault when retrieving info from QSettings

    Currently the number of those objects is a source of confusion And you'd have to sync() all the instances before every use.

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.