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:
Code:
return internalAppSettings->value("main/info", "default").toString();
}
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:
Code:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1225369392 (LWP 17790)]
0x08063322 in settings::getSomeInfo (this=0x0) at src/settings.cpp:104
Use the -dograb option to enforce grabbing.
Scope for 104:
Symbol this is a variable with complex or multiple locations (DWARF2), length 4.
(gdb)
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
Thanks in advance ~codeslicer :)
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.
Re: Segfault when retrieving info from QSettings
Initialize? Is this what you mean:
I did this in my constructor.
Re: Segfault when retrieving info from QSettings
I declared it in my header file too..
Code:
private:
//Internal QSettings declaration
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 :eek:
Re: Segfault when retrieving info from QSettings
Quote:
Originally Posted by
codeslicer
This creates a local variable called internalAppSettings that gets destroyed immediately when the constructor returns. Your member variable is left uninitialized. Try this instead:
Or better yet simply don't declare "internalAppSettings" as a pointer but instead as a regular object and then leave the constructor empty.
Re: Segfault when retrieving info from QSettings
Thanks! I didn't know that the variable gets deleted.
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...
Re: Segfault when retrieving info from QSettings
Just in case, attached is the relevant code:
main.cpp
Code:
int main(int argc, char ** argv)
{
...
...
return app.exec();
}
settings.cpp
Code:
#include <QtCore>
#include "settings.h"
}
...
return internalAppSettings->value("main/info", "default").toString();
}
settings.h
Code:
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QWidget>
#include <QSettings>
{
Q_OBJECT
public:
...
...
private:
//Internal QSettings declaration
};
#endif
Personally I don't see anything wrong with this... Yet I still get a segfault:
Code:
Symbol this is a variable with complex or multiple locations (DWARF2), length 4.
This is really weird ;) Any hints?
Re: Segfault when retrieving info from QSettings
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.
Code:
void C::m(p){
//...
}
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...
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.
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!
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.
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!
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.
Re: Segfault when retrieving info from QSettings
Sorry.. what do you mean by singleton?
Again, thanks for your help:)
Re: Segfault when retrieving info from QSettings
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! :p
Re: Segfault when retrieving info from QSettings
You have null pointers and the singleton will eliminate that. Take a look at this:
Code:
public:
Settings *instance() {
static Settings *m_instance = new Settings;
return m_instance;
}
//..
protected:
};
void someClass::someMethod(){
int v = Settings::instance()->value("param", 7).toInt();
}