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. QCache is not allowing to add item when maxcost is reached and QCache API Object crashes
Forum Updated to NodeBB v4.3 + New Features

QCache is not allowing to add item when maxcost is reached and QCache API Object crashes

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 292 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
    sumanth_sarvepalli
    wrote on 26 Apr 2024, 06:26 last edited by
    #1

    QCache is not allowing to add item when maxcost is reached, from QCache documention " QCache automatically takes ownership of the objects that are inserted into the cache and deletes them to make room for new objects, if necessary ", so it is expected that the new data insertion should be happening automatically when tried to insert new data beyond cache maxcost().

    Also, the Object() API crashes when used in different class, consider following example:

    file1.h

    Class A
    {
    public:
          A();
          QCache<int, QString> myCache;
          void insertData();
    private:
          QStringList dataList;
    };
    

    file1.cpp

    #include "file1.h"
    #define CACHE_SIZE 30
    A::A()
    {
        myCache.setMaxCost(CACHE_SIZE);
    }
    
    void A::insertData()
    {
        for (int index = 0; index < 30; index++)
        {    
          // dataList has data inserted separately
           QString dataString(dataList[i]); 
           myCache.insert(index, &dataString,1);
        }
    }
    

    file2.h

    Class B
    {
    public:
          void displayData();
    };
    

    file2.cpp

    #include "file1.h"
    #include "file2.h"
    
    void B::fun()
    {
        QStringList data;
        for (int index = 0; index < 30; index++)
        {    
            data[index] = *m_userLogCache.object(index);
            qDebug() << data(index);
        }
    }
    
    J 1 Reply Last reply 26 Apr 2024, 06:37
    0
    • S sumanth_sarvepalli
      26 Apr 2024, 06:26

      QCache is not allowing to add item when maxcost is reached, from QCache documention " QCache automatically takes ownership of the objects that are inserted into the cache and deletes them to make room for new objects, if necessary ", so it is expected that the new data insertion should be happening automatically when tried to insert new data beyond cache maxcost().

      Also, the Object() API crashes when used in different class, consider following example:

      file1.h

      Class A
      {
      public:
            A();
            QCache<int, QString> myCache;
            void insertData();
      private:
            QStringList dataList;
      };
      

      file1.cpp

      #include "file1.h"
      #define CACHE_SIZE 30
      A::A()
      {
          myCache.setMaxCost(CACHE_SIZE);
      }
      
      void A::insertData()
      {
          for (int index = 0; index < 30; index++)
          {    
            // dataList has data inserted separately
             QString dataString(dataList[i]); 
             myCache.insert(index, &dataString,1);
          }
      }
      

      file2.h

      Class B
      {
      public:
            void displayData();
      };
      

      file2.cpp

      #include "file1.h"
      #include "file2.h"
      
      void B::fun()
      {
          QStringList data;
          for (int index = 0; index < 30; index++)
          {    
              data[index] = *m_userLogCache.object(index);
              qDebug() << data(index);
          }
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Apr 2024, 06:37 last edited by
      #2

      @sumanth_sarvepalli said in QCache is not allowing to add item when maxcost is reached and QCache API Object crashes:

      QString dataString(dataList[i]);
      myCache.insert(index, &dataString,1);

      You are inserting stack allocated objects which are deleted as soon as they go out of scope. So, QCache is trying to delete already freed memory.

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

      S 1 Reply Last reply 21 May 2024, 06:27
      6
      • J jsulm referenced this topic on 26 Apr 2024, 06:56
      • S sumanth_sarvepalli has marked this topic as solved on 21 May 2024, 06:20
      • J jsulm
        26 Apr 2024, 06:37

        @sumanth_sarvepalli said in QCache is not allowing to add item when maxcost is reached and QCache API Object crashes:

        QString dataString(dataList[i]);
        myCache.insert(index, &dataString,1);

        You are inserting stack allocated objects which are deleted as soon as they go out of scope. So, QCache is trying to delete already freed memory.

        S Offline
        S Offline
        sumanth_sarvepalli
        wrote on 21 May 2024, 06:27 last edited by
        #3

        @jsulm Thanks!

        1 Reply Last reply
        0
        • S sumanth_sarvepalli has marked this topic as solved on 21 May 2024, 06:29

        2/3

        26 Apr 2024, 06:37

        • Login

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