QCache is not allowing to add item when maxcost is reached and QCache API Object crashes
-
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); } }
-
@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.
-
-
-