Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSettings crashes immediately
Forum Updated to NodeBB v4.3 + New Features

QSettings crashes immediately

Scheduled Pinned Locked Moved Solved General and Desktop
qsettings
6 Posts 4 Posters 722 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SavizS Offline
    SavizS Offline
    Saviz
    wrote on last edited by
    #1

    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.

    Cobra91151C JonBJ 2 Replies Last reply
    0
    • SavizS Saviz

      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.

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #2

      @Saviz

      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(
      
      1 Reply Last reply
      2
      • SavizS Saviz

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Saviz
        Your QSettings* settings local variable in method_Initialize() shadows member variable Perferences::settings, which remains uninitialized?

        Your compiler warning level should have allowed a warning to be shown?

        SGaistS SavizS 2 Replies Last reply
        2
        • JonBJ JonB

          @Saviz
          Your QSettings* settings local variable in method_Initialize() shadows member variable Perferences::settings, which remains uninitialized?

          Your compiler warning level should have allowed a warning to be shown?

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Beside the good points made by my fellows, you don't need to keep a QSettings member variable, especially the way you use it. You can simply allocate it in you methods and use it as shown in the documentation.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          SavizS 1 Reply Last reply
          3
          • SavizS Saviz has marked this topic as solved on
          • JonBJ JonB

            @Saviz
            Your QSettings* settings local variable in method_Initialize() shadows member variable Perferences::settings, which remains uninitialized?

            Your compiler warning level should have allowed a warning to be shown?

            SavizS Offline
            SavizS Offline
            Saviz
            wrote on last edited by Saviz
            #5

            @JonB You are correct. However, the funny thing is it actually doesn't give a warning for some reason. I don't know how I managed to miss this! I guess it has been a long day :)

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Beside the good points made by my fellows, you don't need to keep a QSettings member variable, especially the way you use it. You can simply allocate it in you methods and use it as shown in the documentation.

              SavizS Offline
              SavizS Offline
              Saviz
              wrote on last edited by
              #6

              @SGaist That is a very good point. I will change it.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved