QSettings crashes immediately
-
I have a class called "Preferences". I try to use this class to save and read from an INI file to manage the settings of my application. In this class I have a "QSettings" object that I create on the heap (I need it to stay alive because I use it later on in my class). I use it to get some information from an INI file.
Preferences.h:
class Perferences : public QObject { Q_OBJECT public: explicit Perferences( QObject *parent = nullptr ); public: ~Perferences( ); private: QSettings* settings; private: void method_Initialize( ); void method_LoadValues( ); };
Preferences.cpp:
#include "preferences.h" Perferences::Perferences(QObject *parent) : QObject{parent} { method_Initialize(); } Perferences::~Perferences() { } void Perferences::method_Initialize() { QSettings* settings = new QSettings( QApplication::applicationDirPath() + "/preferences/preferences.ini", QSettings::Format::IniFormat, this ); if(!settings->contains("PATH_INPUT_VIDEOLOCATION")) { settings->setValue( "PATH_INPUT_VIDEOLOCATION", "NoValue" ); settings->sync(); } // Until this line everything is fine. The file and the key-value is created. // The crash occurs in the following method: method_LoadValues(); } void Perferences::method_LoadValues() { try { QVariant output = settings->value( "PATH_INPUT_VIDEOLOCATION", QStandardPaths::writableLocation(QStandardPaths::MoviesLocation) ); if(!output.isValid()) { qDebug() << "Not valid. Something went wrong."; } } catch (const std::exception& e) { qWarning() << e.what(); } }
The issue is, as soon as I attempt to get the value from settings (using the method settings->value()) my application immediately crashes.
I event checked the INI file:
[General] PATH_INPUT_VIDEOLOCATION=NoValue
Any help is highly appreciated.
-
Hello!
From the first look at your code, you create
QSettings* settings;
twice. The first one is the class member variable and the second it is the local pointer variable.Code:
private: QSettings* settings; void Perferences::method_Initialize() { QSettings* settings = new QSettings(
I think, this could lead to crash. I recommend you use the class member variable and remove the local pointer variable from
method_Initialize
.Code:
private: QSettings* settings; void Perferences::method_Initialize() { settings = new QSettings(
-