Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Segfault when retrieving info from QSettings

  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.

  14. #13
    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!

  15. #14
    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

    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.

  16. #15
    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

    I tried that but I got the error. Again, I'm using this custom settings class because I need some of the members to modify the information before continuing. All the relevant code is above. I'm willing to do anything, as long as I don't need to create a QSettings object for each member.

    By the errors being different I meant in different parts of the code, ie in other members.


    Thanks!

  17. #16
    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

    So why don't you run the debugger and analize the backtrace the same way as before?

    I'd really suggest taking the singleton approach. You'll spend hours debugging the code and you could achieve the same result just less error prone by making your settings class a singleton with a static member returning the instance.

  18. #17
    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.. what do you mean by singleton?

    Again, thanks for your help

  19. #18
    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


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

    codeslicer (21st March 2008)

  21. #19
    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, but how does that help me? I have found out that the segfault originates when accessing ANY member function of QSettings. So this isn't a problem reading from the actual physical source, but from accessing the actual QSettings object. I have used these types of pointers before, nothing bad happened though... I'll check my source again.

    Thanks!

  22. #20
    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 null pointers and the singleton will eliminate that. Take a look at this:

    Qt Code:
    1. class Settings : public QSettings {
    2. public:
    3. Settings *instance() {
    4. static Settings *m_instance = new Settings;
    5. return m_instance;
    6. }
    7. //..
    8. protected:
    9. Settings() : QSettings(qApp){}
    10. };
    11.  
    12. void someClass::someMethod(){
    13. int v = Settings::instance()->value("param", 7).toInt();
    14. }
    To copy to clipboard, switch view to plain text mode 

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

    codeslicer (21st March 2008)

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.