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.
  • sumanth_sarvepalliS Offline
    sumanth_sarvepalliS Offline
    sumanth_sarvepalli
    wrote on 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);
        }
    }
    
    jsulmJ 1 Reply Last reply
    0
    • sumanth_sarvepalliS sumanth_sarvepalli

      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);
          }
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      sumanth_sarvepalliS 1 Reply Last reply
      6
      • jsulmJ jsulm referenced this topic on
      • sumanth_sarvepalliS sumanth_sarvepalli has marked this topic as solved on
      • jsulmJ jsulm

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

        sumanth_sarvepalliS Offline
        sumanth_sarvepalliS Offline
        sumanth_sarvepalli
        wrote on last edited by
        #3

        @jsulm Thanks!

        1 Reply Last reply
        0
        • sumanth_sarvepalliS sumanth_sarvepalli has marked this topic as solved on

        • Login

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