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
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.

  2. #2
    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.

  3. #3
    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.

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

    codeslicer (16th March 2008)

  6. #5
    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.

  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

    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...

  8. #7
    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

  9. #8
    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?

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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 

  11. #10
    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...

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.

  13. #12
    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

    Look, all I want to do is creat my own custom settings object, with members corresponding to different values. Later in my app, instead of declaring QSettings every time I need to use it, I could just create my settings object and just use settings.setValueOfSomething.

    This is helpful because in some of my members I have the function process the input/output before returning/writing to disk.

    Back on track, is this segfault preventable? Thanks!

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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

    Why don't you have a single settings object then which you will either pass as a pointer to every component that is to use it or make it a singleton? Your problems would instantly go away.

    If you want me to help you with the current bad design, you have to at least tell me what the errors are, not only that they are different from previous ones.

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
  •  
Qt is a trademark of The Qt Company.