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 745 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.
  • S Offline
    S Offline
    Saviz
    wrote on 17 Mar 2023, 18:39 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.

    C J 2 Replies Last reply 17 Mar 2023, 18:52
    0
    • S Saviz
      17 Mar 2023, 18:39

      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.

      C Offline
      C Offline
      Cobra91151
      wrote on 17 Mar 2023, 18:52 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
      • S Saviz
        17 Mar 2023, 18:39

        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.

        J Offline
        J Offline
        JonB
        wrote on 17 Mar 2023, 18:53 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?

        S S 2 Replies Last reply 17 Mar 2023, 19:31
        2
        • J JonB
          17 Mar 2023, 18:53

          @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?

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Mar 2023, 19:31 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

          S 1 Reply Last reply 18 Mar 2023, 04:09
          3
          • S Saviz has marked this topic as solved on 18 Mar 2023, 04:07
          • J JonB
            17 Mar 2023, 18:53

            @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?

            S Offline
            S Offline
            Saviz
            wrote on 18 Mar 2023, 04:08 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
            • S SGaist
              17 Mar 2023, 19:31

              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.

              S Offline
              S Offline
              Saviz
              wrote on 18 Mar 2023, 04:09 last edited by
              #6

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

              1 Reply Last reply
              0

              4/6

              17 Mar 2023, 19:31

              • Login

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