Search code examples
c++qtmemoryqtcoreqt5.6

Qcache doesn't store anything in Qt 5.6


I'm trying to store QStrings in the QCache but after I close the application and run it again, the cache is empty again.

here is my cpp code:

QString fileName = QFileDialog::getOpenFileName(this, tr("Select a ply file"), homeDir, ".ply (*.ply)");
fileCache.insert(12, &fileName);
qDebug() << QString("# items in cache: ") + QString::number(fileCache.count());
ui->file_name_textEdit->insertPlainText(fileName);

and I have the cache declared in the header:

QCache<int, QString> fileCache;

I have also tried storing a tmp value because before i was receiving this memory leak error:

free(): invalid size: 0x00007fffa0d96600 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7fed3ebc1725]
/lib/x86_64-linux-gnu/libc.so.6(+0x7ff4a)[0x7fed3ebc9f4a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fed3ebcdabc]
/home/pendar/projects/paintsquad/build/pcl_visualizer(_ZN6QCacheIi7QStringE5clearEv+0x39)[0x473b0f]
/home/pendar/projects/paintsquad/build/pcl_visualizer(_ZN6QCacheIi7QStringED1Ev+0x19)[0x46f3d1]
/home/pendar/projects/paintsquad/build/pcl_visualizer(_ZN9PCLViewerD1Ev+0x6a)[0x468532]
/home/pendar/projects/paintsquad/build/pcl_visualizer(main+0x83)[0x463c79]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fed3eb6a830]
/home/pendar/projects/paintsquad/build/pcl_visualizer(_start+0x29)[0x463b29]

So I tried this:

QString *tmp = new QString("hi");
fileCache.insert(12, tmp);

Now I'm not getting any errors, but the cache is still empty on restart!


Solution

  • So I needed to use QSettings instead, which provides persistancy across closing and opening the application.