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. uninitialized switchcase error
Forum Updated to NodeBB v4.3 + New Features

uninitialized switchcase error

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 629 Views
  • 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
    saber
    wrote on last edited by
    #1

    this is my function.

    void coreaction::batteryCheck()
    {
        if (sm.getShowBattery()) {
            UPower *u = new UPower(this);
            
            Battery *b;
            foreach (Battery *bat, *u->batteries()) {
                b = u->batteries()->value(bat->path());
            }
            ui->batteryProg->setValue(b->percentage());
    
            switch( b->state() ) {
                case Battery::FullyCharged:
                    ui->batteryStatus->setText( tr( "Full" ) );
                    break;
                case Battery::Discharging:
                    ui->batteryStatus->setText( tr( "Discharging" ) );
                    break;
                case Battery::Charging:
                    ui->batteryStatus->setText( tr( "Charging" ) );
                    break;
                default:
                    ui->batteryStatus->setText( tr( "No Battery" ) );
                    break;
            }
            ui->batteryframe->setVisible(true);
        } else {
            ui->batteryframe->setVisible(false);
        }
    }
    

    battery.h

    #ifndef BATTERY_H
    #define BATTERY_H
    
    #include <QObject>
    
    
    class QDBusInterface;
    
    class Battery : public QObject {
    
        Q_OBJECT
    
        Q_ENUMS(State)
        Q_PROPERTY(QString sysfsPath READ sysfsPath STORED false)
        Q_PROPERTY(QString model READ model STORED false)
        Q_PROPERTY(QString vendor READ vendor STORED false)
        Q_PROPERTY(QString technology READ technology STORED false)
        Q_PROPERTY(QString path READ path STORED false)
        Q_PROPERTY(bool powerSupply READ powerSupply STORED false)
        Q_PROPERTY(bool hasHistory READ hasHistory STORED false)
        Q_PROPERTY(bool hasStatistics READ hasStatistics STORED false)
        Q_PROPERTY(bool isPresent READ isPresent STORED false)
        Q_PROPERTY(bool isRechargeable READ isRechargeable STORED false)
        Q_PROPERTY(double energy READ energy STORED false)
        Q_PROPERTY(double energyEmpty READ energyEmpty STORED false)
        Q_PROPERTY(double energyFull READ energyFull STORED false)
        Q_PROPERTY(double energyFullDesign READ energyFullDesign STORED false)
        Q_PROPERTY(double energyRate READ energyRate STORED false)
        Q_PROPERTY(double voltage READ voltage STORED false)
        Q_PROPERTY(double percentage READ percentage STORED false)
        Q_PROPERTY(double capacity READ capacity STORED false)
        Q_PROPERTY(State state READ state STORED false)
        Q_PROPERTY(int lowLevel READ lowLevel)
        Q_PROPERTY(double toFull READ toFull STORED false)
        Q_PROPERTY(double toEmpty READ toFull STORED false)
    
    public:
        Battery(const QString & path, QObject *parent = 0);
        ~Battery();
    
        /* Enums */
        enum State {FullyCharged, Charging, Discharging};
    
        /* Properties */
        QString sysfsPath() const;
        QString model() const;
        QString vendor() const;
        QString technology() const;
        const QString & path() const;
    
        bool powerSupply() const;
        bool hasHistory() const;
        bool hasStatistics() const;
        bool isPresent() const;
        bool isRechargeable() const;
        bool isValid() const;
    
        double energy() const;
        double energyEmpty() const;
        double energyFull() const;
        double energyFullDesign() const;
        double energyRate() const;
        double voltage() const;
        double percentage() const;
        double capacity() const;
        double toFull() const;
        double toEmpty() const;
        int lowLevel() const;
        void setLowLevel(int value);
    
        Battery::State state() const;
    
    private:
        void createInterface();
    
        QDBusInterface      *m_interface;
        QString             m_path;
        bool                m_hasAlreadyBeenLow;
        bool                m_hasAlreadyBeenFull;
        int                 m_lowLevel;
        bool                m_valid;
    
    public Q_SLOTS:
        void refresh();
    
    private Q_SLOTS:
        void update();
    
    Q_SIGNALS:
        void changed();
        void low();
        void full();
    };
    

    this is battery.cpp

    #include "battery.h"
    
    #include <QtDBus/QDBusConnection>
    #include <QtDBus/QDBusInterface>
    #include <QSettings>
    
    
    Battery::Battery(const QString &path, QObject *parent)
        : QObject(parent), m_interface(0),
          m_path(path), m_hasAlreadyBeenLow(false),
          m_hasAlreadyBeenFull(false), m_valid(false)  {
    
        createInterface();
    }
    
    Battery::~Battery() {
    
    }
    
    void Battery::createInterface() {
        if(m_interface==0 || !m_interface->isValid()) {
            m_interface = new QDBusInterface("org.freedesktop.UPower", m_path,
                                             "org.freedesktop.UPower.Device", QDBusConnection::systemBus(), this);
            connect(m_interface, SIGNAL(Changed()), this, SIGNAL(changed()));
            if(!m_interface->isValid()) {
                delete m_interface;
                m_interface = 0;
                m_valid = false;
                return;
            }
        }
    
        m_valid = true;
    }
    
    void Battery::refresh() {
        m_interface->call("Refresh");
    }
    
    void Battery::update() {
        int level = percentage();
    
        if(level<lowLevel()) {
            if(!m_hasAlreadyBeenLow) {
                m_hasAlreadyBeenLow = true;
                emit low();
            }
        } else {
            m_hasAlreadyBeenLow = false;
        }
    
        if(state() == FullyCharged) {
            if(!m_hasAlreadyBeenFull) {
                m_hasAlreadyBeenFull = true;
                emit full();
            }
        } else {
            m_hasAlreadyBeenFull = false;
        }
    }
    
    bool Battery::isValid() const {
        return m_interface->property("Type").toInt() == 2 && m_valid;
    }
    
    int Battery::lowLevel() const {
        QSettings settings;
        settings.beginGroup("Batteries_Low_Level");
            int lvl = settings.value(m_path, 20).toInt();
        settings.endGroup();
        return lvl;
    }
    
    void Battery::setLowLevel(int value) {
        QSettings settings;
        settings.beginGroup("Batteries_Low_Level");
            settings.setValue(m_path, value);
        settings.endGroup();
    }
    
    QString Battery::sysfsPath() const {
        return m_interface->property("NativePath").toString();
    }
    
    QString Battery::model() const {
        return m_interface->property("Model").toString();
    }
    
    QString Battery::vendor() const {
        return m_interface->property("Vendor").toString();
    }
    
    QString Battery::technology() const {
        return m_interface->property("Technology").toString();
    }
    
    const QString & Battery::path() const {
        return m_path;
    }
    
    bool Battery::powerSupply() const {
        return m_interface->property("PowerSupply").toBool();
    }
    
    bool Battery::hasHistory() const {
        return m_interface->property("HasHistory").toBool();
    }
    
    bool Battery::hasStatistics() const {
        return m_interface->property("HasStatistics").toBool();
    }
    
    bool Battery::isPresent() const {
        return m_interface->property("IsPresent").toBool();
    }
    
    bool Battery::isRechargeable() const {
        return m_interface->property("IsRechargeable").toBool();
    }
    
    double Battery::energy() const {
        return m_interface->property("Energy").toDouble();
    }
    
    double Battery::energyEmpty() const {
        return m_interface->property("EnergyEmpty").toDouble();
    }
    
    double Battery::energyFull() const {
        return m_interface->property("EnergyFull").toDouble();
    }
    
    double Battery::energyFullDesign() const {
        return m_interface->property("EnergyFullDesign").toDouble();
    }
    
    double Battery::energyRate() const {
        return m_interface->property("EnergyRate").toDouble();
    }
    
    double Battery::voltage() const {
        return m_interface->property("Voltage").toDouble();
    }
    
    double Battery::percentage() const {
        return m_interface->property("Percentage").toDouble();
    }
    
    double Battery::capacity() const {
        return m_interface->property("Capacity").toDouble();
    }
    
    double Battery::toEmpty() const {
        return m_interface->property("TimeToEmpty").toDouble();
    }
    
    double Battery::toFull() const {
        return m_interface->property("TimeToFull").toDouble();
    }
    
    Battery::State Battery::state() const {
        uint state = m_interface->property("State").toUInt();
        switch( state ) {
            case (uint) 0:
                return FullyCharged;
                break;
    
            case (uint) 1:
                return Charging;
                break;
    
            case (uint) 2:
                return Discharging;
                break;
    
            default:
                return FullyCharged;
                break;
        }
    }
    
    

    problem is when i compile i got this error

    g++ -c -pipe -O2 -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -O2 -pipe -fstack-protector-strong -fno-plt -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_PRINTSUPPORT_LIB -DQT_MULTIMEDIAWIDGETS_LIB -DQT_CHARTS_LIB -DQT_WIDGETS_LIB -DQT_MULTIMEDIA_LIB -DQT_GUI_LIB -DQT_DBUS_LIB -DQT_NETWORK_LIB -DQT_CONCURRENT_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt -isystem /usr/include/qt/QtPrintSupport -isystem /usr/include/qt/QtMultimediaWidgets -isystem /usr/include/qt/QtCharts -isystem /usr/include/qt/QtWidgets -isystem /usr/include/qt/QtMultimedia -isystem /usr/include/qt/QtGui -isystem /usr/include/qt/QtDBus -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtConcurrent -isystem /usr/include/qt/QtCore -I. -isystem /usr/include/libdrm -I. -I/usr/lib/qt/mkspecs/linux-g++ -o mimeutils.o corefm/mimeutils.cpp
    make: *** [Makefile:2028: corebox.o] Error 1
    make: *** Waiting for unfinished jobs....
    coreaction/coreaction.cpp: In member function 'void coreaction::batteryCheck()':
    coreaction/coreaction.cpp:114:25: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
             switch( b->state() ) {
                     ~~~~~~~~^~
    ==> ERROR: A failure occurred in build().
        Aborting...
    

    what i missing??

    jsulmJ 1 Reply Last reply
    0
    • S saber

      this is my function.

      void coreaction::batteryCheck()
      {
          if (sm.getShowBattery()) {
              UPower *u = new UPower(this);
              
              Battery *b;
              foreach (Battery *bat, *u->batteries()) {
                  b = u->batteries()->value(bat->path());
              }
              ui->batteryProg->setValue(b->percentage());
      
              switch( b->state() ) {
                  case Battery::FullyCharged:
                      ui->batteryStatus->setText( tr( "Full" ) );
                      break;
                  case Battery::Discharging:
                      ui->batteryStatus->setText( tr( "Discharging" ) );
                      break;
                  case Battery::Charging:
                      ui->batteryStatus->setText( tr( "Charging" ) );
                      break;
                  default:
                      ui->batteryStatus->setText( tr( "No Battery" ) );
                      break;
              }
              ui->batteryframe->setVisible(true);
          } else {
              ui->batteryframe->setVisible(false);
          }
      }
      

      battery.h

      #ifndef BATTERY_H
      #define BATTERY_H
      
      #include <QObject>
      
      
      class QDBusInterface;
      
      class Battery : public QObject {
      
          Q_OBJECT
      
          Q_ENUMS(State)
          Q_PROPERTY(QString sysfsPath READ sysfsPath STORED false)
          Q_PROPERTY(QString model READ model STORED false)
          Q_PROPERTY(QString vendor READ vendor STORED false)
          Q_PROPERTY(QString technology READ technology STORED false)
          Q_PROPERTY(QString path READ path STORED false)
          Q_PROPERTY(bool powerSupply READ powerSupply STORED false)
          Q_PROPERTY(bool hasHistory READ hasHistory STORED false)
          Q_PROPERTY(bool hasStatistics READ hasStatistics STORED false)
          Q_PROPERTY(bool isPresent READ isPresent STORED false)
          Q_PROPERTY(bool isRechargeable READ isRechargeable STORED false)
          Q_PROPERTY(double energy READ energy STORED false)
          Q_PROPERTY(double energyEmpty READ energyEmpty STORED false)
          Q_PROPERTY(double energyFull READ energyFull STORED false)
          Q_PROPERTY(double energyFullDesign READ energyFullDesign STORED false)
          Q_PROPERTY(double energyRate READ energyRate STORED false)
          Q_PROPERTY(double voltage READ voltage STORED false)
          Q_PROPERTY(double percentage READ percentage STORED false)
          Q_PROPERTY(double capacity READ capacity STORED false)
          Q_PROPERTY(State state READ state STORED false)
          Q_PROPERTY(int lowLevel READ lowLevel)
          Q_PROPERTY(double toFull READ toFull STORED false)
          Q_PROPERTY(double toEmpty READ toFull STORED false)
      
      public:
          Battery(const QString & path, QObject *parent = 0);
          ~Battery();
      
          /* Enums */
          enum State {FullyCharged, Charging, Discharging};
      
          /* Properties */
          QString sysfsPath() const;
          QString model() const;
          QString vendor() const;
          QString technology() const;
          const QString & path() const;
      
          bool powerSupply() const;
          bool hasHistory() const;
          bool hasStatistics() const;
          bool isPresent() const;
          bool isRechargeable() const;
          bool isValid() const;
      
          double energy() const;
          double energyEmpty() const;
          double energyFull() const;
          double energyFullDesign() const;
          double energyRate() const;
          double voltage() const;
          double percentage() const;
          double capacity() const;
          double toFull() const;
          double toEmpty() const;
          int lowLevel() const;
          void setLowLevel(int value);
      
          Battery::State state() const;
      
      private:
          void createInterface();
      
          QDBusInterface      *m_interface;
          QString             m_path;
          bool                m_hasAlreadyBeenLow;
          bool                m_hasAlreadyBeenFull;
          int                 m_lowLevel;
          bool                m_valid;
      
      public Q_SLOTS:
          void refresh();
      
      private Q_SLOTS:
          void update();
      
      Q_SIGNALS:
          void changed();
          void low();
          void full();
      };
      

      this is battery.cpp

      #include "battery.h"
      
      #include <QtDBus/QDBusConnection>
      #include <QtDBus/QDBusInterface>
      #include <QSettings>
      
      
      Battery::Battery(const QString &path, QObject *parent)
          : QObject(parent), m_interface(0),
            m_path(path), m_hasAlreadyBeenLow(false),
            m_hasAlreadyBeenFull(false), m_valid(false)  {
      
          createInterface();
      }
      
      Battery::~Battery() {
      
      }
      
      void Battery::createInterface() {
          if(m_interface==0 || !m_interface->isValid()) {
              m_interface = new QDBusInterface("org.freedesktop.UPower", m_path,
                                               "org.freedesktop.UPower.Device", QDBusConnection::systemBus(), this);
              connect(m_interface, SIGNAL(Changed()), this, SIGNAL(changed()));
              if(!m_interface->isValid()) {
                  delete m_interface;
                  m_interface = 0;
                  m_valid = false;
                  return;
              }
          }
      
          m_valid = true;
      }
      
      void Battery::refresh() {
          m_interface->call("Refresh");
      }
      
      void Battery::update() {
          int level = percentage();
      
          if(level<lowLevel()) {
              if(!m_hasAlreadyBeenLow) {
                  m_hasAlreadyBeenLow = true;
                  emit low();
              }
          } else {
              m_hasAlreadyBeenLow = false;
          }
      
          if(state() == FullyCharged) {
              if(!m_hasAlreadyBeenFull) {
                  m_hasAlreadyBeenFull = true;
                  emit full();
              }
          } else {
              m_hasAlreadyBeenFull = false;
          }
      }
      
      bool Battery::isValid() const {
          return m_interface->property("Type").toInt() == 2 && m_valid;
      }
      
      int Battery::lowLevel() const {
          QSettings settings;
          settings.beginGroup("Batteries_Low_Level");
              int lvl = settings.value(m_path, 20).toInt();
          settings.endGroup();
          return lvl;
      }
      
      void Battery::setLowLevel(int value) {
          QSettings settings;
          settings.beginGroup("Batteries_Low_Level");
              settings.setValue(m_path, value);
          settings.endGroup();
      }
      
      QString Battery::sysfsPath() const {
          return m_interface->property("NativePath").toString();
      }
      
      QString Battery::model() const {
          return m_interface->property("Model").toString();
      }
      
      QString Battery::vendor() const {
          return m_interface->property("Vendor").toString();
      }
      
      QString Battery::technology() const {
          return m_interface->property("Technology").toString();
      }
      
      const QString & Battery::path() const {
          return m_path;
      }
      
      bool Battery::powerSupply() const {
          return m_interface->property("PowerSupply").toBool();
      }
      
      bool Battery::hasHistory() const {
          return m_interface->property("HasHistory").toBool();
      }
      
      bool Battery::hasStatistics() const {
          return m_interface->property("HasStatistics").toBool();
      }
      
      bool Battery::isPresent() const {
          return m_interface->property("IsPresent").toBool();
      }
      
      bool Battery::isRechargeable() const {
          return m_interface->property("IsRechargeable").toBool();
      }
      
      double Battery::energy() const {
          return m_interface->property("Energy").toDouble();
      }
      
      double Battery::energyEmpty() const {
          return m_interface->property("EnergyEmpty").toDouble();
      }
      
      double Battery::energyFull() const {
          return m_interface->property("EnergyFull").toDouble();
      }
      
      double Battery::energyFullDesign() const {
          return m_interface->property("EnergyFullDesign").toDouble();
      }
      
      double Battery::energyRate() const {
          return m_interface->property("EnergyRate").toDouble();
      }
      
      double Battery::voltage() const {
          return m_interface->property("Voltage").toDouble();
      }
      
      double Battery::percentage() const {
          return m_interface->property("Percentage").toDouble();
      }
      
      double Battery::capacity() const {
          return m_interface->property("Capacity").toDouble();
      }
      
      double Battery::toEmpty() const {
          return m_interface->property("TimeToEmpty").toDouble();
      }
      
      double Battery::toFull() const {
          return m_interface->property("TimeToFull").toDouble();
      }
      
      Battery::State Battery::state() const {
          uint state = m_interface->property("State").toUInt();
          switch( state ) {
              case (uint) 0:
                  return FullyCharged;
                  break;
      
              case (uint) 1:
                  return Charging;
                  break;
      
              case (uint) 2:
                  return Discharging;
                  break;
      
              default:
                  return FullyCharged;
                  break;
          }
      }
      
      

      problem is when i compile i got this error

      g++ -c -pipe -O2 -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -O2 -pipe -fstack-protector-strong -fno-plt -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_PRINTSUPPORT_LIB -DQT_MULTIMEDIAWIDGETS_LIB -DQT_CHARTS_LIB -DQT_WIDGETS_LIB -DQT_MULTIMEDIA_LIB -DQT_GUI_LIB -DQT_DBUS_LIB -DQT_NETWORK_LIB -DQT_CONCURRENT_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt -isystem /usr/include/qt/QtPrintSupport -isystem /usr/include/qt/QtMultimediaWidgets -isystem /usr/include/qt/QtCharts -isystem /usr/include/qt/QtWidgets -isystem /usr/include/qt/QtMultimedia -isystem /usr/include/qt/QtGui -isystem /usr/include/qt/QtDBus -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtConcurrent -isystem /usr/include/qt/QtCore -I. -isystem /usr/include/libdrm -I. -I/usr/lib/qt/mkspecs/linux-g++ -o mimeutils.o corefm/mimeutils.cpp
      make: *** [Makefile:2028: corebox.o] Error 1
      make: *** Waiting for unfinished jobs....
      coreaction/coreaction.cpp: In member function 'void coreaction::batteryCheck()':
      coreaction/coreaction.cpp:114:25: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
               switch( b->state() ) {
                       ~~~~~~~~^~
      ==> ERROR: A failure occurred in build().
          Aborting...
      

      what i missing??

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @saber Take a look at this code:

      Battery *b;
      foreach (Battery *bat, *u->batteries()) {
          b = u->batteries()->value(bat->path());
      }
      

      If u->batteries() returns an empty list b will be a dangling pointer and your app will crash. That's what compiler is telling you.
      To be honest I don't understand this code: why do you iterate in a loop over the whole list but only keep the last value in b? It would be way faster to directly access the last element.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      3
      • jsulmJ jsulm

        @saber Take a look at this code:

        Battery *b;
        foreach (Battery *bat, *u->batteries()) {
            b = u->batteries()->value(bat->path());
        }
        

        If u->batteries() returns an empty list b will be a dangling pointer and your app will crash. That's what compiler is telling you.
        To be honest I don't understand this code: why do you iterate in a loop over the whole list but only keep the last value in b? It would be way faster to directly access the last element.

        S Offline
        S Offline
        saber
        wrote on last edited by
        #3

        @jsulm

        please can u show the right way to do this .
        please .
        it is done by my friend and he is not in touch .
        i need the fix badly.

        jsulmJ 1 Reply Last reply
        0
        • S saber

          @jsulm

          please can u show the right way to do this .
          please .
          it is done by my friend and he is not in touch .
          i need the fix badly.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @saber This is really not difficult:

          Battery *b = nullptr;
          foreach (Battery *bat, *u->batteries()) {
              b = u->batteries()->value(bat->path());
          }
          if (!b)
              return;
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          5
          • jsulmJ jsulm

            @saber This is really not difficult:

            Battery *b = nullptr;
            foreach (Battery *bat, *u->batteries()) {
                b = u->batteries()->value(bat->path());
            }
            if (!b)
                return;
            
            S Offline
            S Offline
            saber
            wrote on last edited by saber
            #5

            @jsulm
            thanks for the solution .

            can i invite u to join my project ? i know it is not the way to do that. but my app needs many fix that i can't ask all of them in forum.

            my app is for linux.please check it out CoreBox
            thanks.

            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