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. ASSERT: "!data || data->ref.load() >= 1" in file image\qpixmap.cpp, line 273
Forum Updated to NodeBB v4.3 + New Features

ASSERT: "!data || data->ref.load() >= 1" in file image\qpixmap.cpp, line 273

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 2.6k Views 2 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.
  • joeQJ Offline
    joeQJ Offline
    joeQ
    wrote on last edited by joeQ
    #1

    Hi, friend. I opened a lot of images at once. and then,passed these image paths to some subclass QRunnable to read data. like below snippet code.

        for(int i=0; i<ltfilesPath.size(); i++){
            task = new CreateItemTask(ltfilesPath[i]);
            connect(task,&CreateItemTask::SigCreateItemTaskFinished,
                    this,&MainWindow::StCreateItemTaskFinished);
            _threadPool->start(task);
        }
    

    about my CreateItemTask class, like below code snippet:

    TEITEMTASK_H
    #define CREATEITEMTASK_H
    #include <QtCore/QRunnable>
    #include <QtCore/QObject>
    
    class CreateItemTask : public QObject, public QRunnable
    {
        Q_OBJECT
    public:
        CreateItemTask(const QString &filePath,QObject *parent = Q_NULLPTR);
        ~CreateItemTask();
    
        void run() override;
    
    signals:
        void SigCreateItemTaskFinished(void* item);
    
    private:
        QString     _filePath;
    };
    
    #endif // CREATEITEMTASK_H
    
    
    #include "itemtask.h"
    /** some include here ... */
    
    CreateItemTask::CreateItemTask(const QString &filePath, QObject *parent)
        :QObject(parent),QRunnable()
    {
        _filePath = filePath;
        setAutoDelete(true);
    }
    
    CreateItemTask::~CreateItemTask()
    {
    }
    
    void CreateItemTask::run()
    {
        /** some declare here */
    
        if(!ReadPixmap(_filePath,pixmap)){ /// this function code at below.
            return;
        }
    
        pItem = new QListWidgetItem;
        /** iterm set some data at here... */
    
        /** some code here.... */
    
        emit SigCreateItemTaskFinished((void*)pItem);
    }
    
    

    ReadPixmap Function.

    bool ReadPixmap(QString qsFilePath, QPixmap &pixmap)
    {
        /** some declare here... */
        if(!pixmap.load(qsFilePath)){ ///< Note here...
            reader.setFileName(qsFilePath);
            reader.setDecideFormatFromContent(true);
            if(!reader.canRead()){
                return false;
            }
    
            image = reader.read();
            if(image.isNull()){
                return false;
            }
    
            pixmap = QPixmap::fromImage(image);
            if(pixmap.isNull()){
                return false;
            }
    
        }
        return true;
    }
    

    sometime, pixmap.load(qsFilePath) some image can read crashed, and putout

    ASSERT: "!data || data->ref.load() >= 1" in file image\qpixmap.cpp, line 273

    What's wrong i did? and How to work it out?

    Just do it!

    jsulmJ kshegunovK 2 Replies Last reply
    0
    • joeQJ joeQ

      Hi, friend. I opened a lot of images at once. and then,passed these image paths to some subclass QRunnable to read data. like below snippet code.

          for(int i=0; i<ltfilesPath.size(); i++){
              task = new CreateItemTask(ltfilesPath[i]);
              connect(task,&CreateItemTask::SigCreateItemTaskFinished,
                      this,&MainWindow::StCreateItemTaskFinished);
              _threadPool->start(task);
          }
      

      about my CreateItemTask class, like below code snippet:

      TEITEMTASK_H
      #define CREATEITEMTASK_H
      #include <QtCore/QRunnable>
      #include <QtCore/QObject>
      
      class CreateItemTask : public QObject, public QRunnable
      {
          Q_OBJECT
      public:
          CreateItemTask(const QString &filePath,QObject *parent = Q_NULLPTR);
          ~CreateItemTask();
      
          void run() override;
      
      signals:
          void SigCreateItemTaskFinished(void* item);
      
      private:
          QString     _filePath;
      };
      
      #endif // CREATEITEMTASK_H
      
      
      #include "itemtask.h"
      /** some include here ... */
      
      CreateItemTask::CreateItemTask(const QString &filePath, QObject *parent)
          :QObject(parent),QRunnable()
      {
          _filePath = filePath;
          setAutoDelete(true);
      }
      
      CreateItemTask::~CreateItemTask()
      {
      }
      
      void CreateItemTask::run()
      {
          /** some declare here */
      
          if(!ReadPixmap(_filePath,pixmap)){ /// this function code at below.
              return;
          }
      
          pItem = new QListWidgetItem;
          /** iterm set some data at here... */
      
          /** some code here.... */
      
          emit SigCreateItemTaskFinished((void*)pItem);
      }
      
      

      ReadPixmap Function.

      bool ReadPixmap(QString qsFilePath, QPixmap &pixmap)
      {
          /** some declare here... */
          if(!pixmap.load(qsFilePath)){ ///< Note here...
              reader.setFileName(qsFilePath);
              reader.setDecideFormatFromContent(true);
              if(!reader.canRead()){
                  return false;
              }
      
              image = reader.read();
              if(image.isNull()){
                  return false;
              }
      
              pixmap = QPixmap::fromImage(image);
              if(pixmap.isNull()){
                  return false;
              }
      
          }
          return true;
      }
      

      sometime, pixmap.load(qsFilePath) some image can read crashed, and putout

      ASSERT: "!data || data->ref.load() >= 1" in file image\qpixmap.cpp, line 273

      What's wrong i did? and How to work it out?

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

      @joeQ said in ASSERT: "!data || data->ref.load() >= 1" in file image\qpixmap.cpp, line 273:

      if(!ReadPixmap(_filePath,pixmap))

      Where is pixmap declared? Did you check all the images you're trying to load? Maybe some of them are broken?

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

      1 Reply Last reply
      0
      • joeQJ joeQ

        Hi, friend. I opened a lot of images at once. and then,passed these image paths to some subclass QRunnable to read data. like below snippet code.

            for(int i=0; i<ltfilesPath.size(); i++){
                task = new CreateItemTask(ltfilesPath[i]);
                connect(task,&CreateItemTask::SigCreateItemTaskFinished,
                        this,&MainWindow::StCreateItemTaskFinished);
                _threadPool->start(task);
            }
        

        about my CreateItemTask class, like below code snippet:

        TEITEMTASK_H
        #define CREATEITEMTASK_H
        #include <QtCore/QRunnable>
        #include <QtCore/QObject>
        
        class CreateItemTask : public QObject, public QRunnable
        {
            Q_OBJECT
        public:
            CreateItemTask(const QString &filePath,QObject *parent = Q_NULLPTR);
            ~CreateItemTask();
        
            void run() override;
        
        signals:
            void SigCreateItemTaskFinished(void* item);
        
        private:
            QString     _filePath;
        };
        
        #endif // CREATEITEMTASK_H
        
        
        #include "itemtask.h"
        /** some include here ... */
        
        CreateItemTask::CreateItemTask(const QString &filePath, QObject *parent)
            :QObject(parent),QRunnable()
        {
            _filePath = filePath;
            setAutoDelete(true);
        }
        
        CreateItemTask::~CreateItemTask()
        {
        }
        
        void CreateItemTask::run()
        {
            /** some declare here */
        
            if(!ReadPixmap(_filePath,pixmap)){ /// this function code at below.
                return;
            }
        
            pItem = new QListWidgetItem;
            /** iterm set some data at here... */
        
            /** some code here.... */
        
            emit SigCreateItemTaskFinished((void*)pItem);
        }
        
        

        ReadPixmap Function.

        bool ReadPixmap(QString qsFilePath, QPixmap &pixmap)
        {
            /** some declare here... */
            if(!pixmap.load(qsFilePath)){ ///< Note here...
                reader.setFileName(qsFilePath);
                reader.setDecideFormatFromContent(true);
                if(!reader.canRead()){
                    return false;
                }
        
                image = reader.read();
                if(image.isNull()){
                    return false;
                }
        
                pixmap = QPixmap::fromImage(image);
                if(pixmap.isNull()){
                    return false;
                }
        
            }
            return true;
        }
        

        sometime, pixmap.load(qsFilePath) some image can read crashed, and putout

        ASSERT: "!data || data->ref.load() >= 1" in file image\qpixmap.cpp, line 273

        What's wrong i did? and How to work it out?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        You can't use pixmaps from different threads, you need to switch to QImage when doing multithreading.

        Read and abide by the Qt Code of Conduct

        joeQJ 1 Reply Last reply
        3
        • kshegunovK kshegunov

          You can't use pixmaps from different threads, you need to switch to QImage when doing multithreading.

          joeQJ Offline
          joeQJ Offline
          joeQ
          wrote on last edited by
          #4

          @kshegunov

          Thank you very much.

          I modified the function to

          bool ReadPixmap(QString qsFilePath, QPixmap &pixmap)
          {
              /** some declare at here */
          
              reader.setFileName(qsFilePath);
              reader.setDecideFormatFromContent(true);
              if(!reader.canRead()){
                  return false;
              }
          
              image = reader.read();
              if(image.isNull()){
                  return false;
              }
          
              pixmap = QPixmap::fromImage(image);
              if(pixmap.isNull()){
                  return false;
              }
          
              return true;
          

          It worked.

          Just do it!

          kshegunovK 1 Reply Last reply
          0
          • joeQJ joeQ

            @kshegunov

            Thank you very much.

            I modified the function to

            bool ReadPixmap(QString qsFilePath, QPixmap &pixmap)
            {
                /** some declare at here */
            
                reader.setFileName(qsFilePath);
                reader.setDecideFormatFromContent(true);
                if(!reader.canRead()){
                    return false;
                }
            
                image = reader.read();
                if(image.isNull()){
                    return false;
                }
            
                pixmap = QPixmap::fromImage(image);
                if(pixmap.isNull()){
                    return false;
                }
            
                return true;
            

            It worked.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            You still have a problem as you're creating, accessing and modifying widgets and pixmaps from different threads. QWidget and QPixmap are not reentrant, meaning you can create and access them only from the GUI thread.

            pItem = new QListWidgetItem;
            

            and

            pixmap = QPixmap::fromImage(image);
            

            are the offending lines. You need to modify everything in such a way that the widget creation and the pixmap conversion are done in the GUI thread; namely inside MainWindow::StCreateItemTaskFinished, not in the runnable's implementation.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            2
            • W Offline
              W Offline
              watermelon_seven
              wrote on last edited by
              #6

              Yes, that friend above is right

              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