PDA

View Full Version : Memory usage and QGraphicsView/Scene with QML component



jovin
10th May 2012, 21:07
Hello,

I unsure about where I should place the thread, so please move if it's in the wrong section.

So, now to my problem.
In my Qt program I have a stackedWidget and one page of the widget has a QGraphicsView in it.
Before I enter the QGraphicsView I call a function which sets up my QGraphicsScene and loads a QML file:

stackScene = new MyScene(); //inherits from QGraphicsScene
engine = new QDeclarativeEngine();
stackData = new MyData(); // contains several Vectors
engine->rootContext()->setContextProperty("stackData", stackData);
QDeclarativeComponent component(engine, QUrl(QString::fromUtf8("qrc:QML/stackSceneQML.qml")));
//qDebug() << component.errors() << endl;
object = qobject_cast<QGraphicsObject *>(component.create());
stackScene->addItem(object);

// process to QGraphicsView

It works fine and all and I free all memory allocated with new from above when I leave the QGraphicsView:


delete stackScene;
stackScene = NULL;
// delete engine; commented, because it gives me segmention fault...
// delete object; well same as above
delete stackData;
stackData = NULL;


Now the behaviour I can not explain:
The memory used is about 10 MB when I launch my program, entering the stackWidget with QGraphicsView
adds a few kB - but leaving it doesn't remove even one kB instead I stays and adds more kB when I switch to it again.
I stopped at 20MB...

I hope someone may have a clue about what's going wrong here.

It's really annoying.

jovin
11th May 2012, 19:00
Here is the output of Valgrind:


32 bytes in 2 blocks are definitely lost in loss record 3,883 of 6,918
in FcPatternCreate in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4
1: malloc in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so
2: FcPatternCreate in /usr/lib/i386-linux-gnu/libfontconfig.so.1.4.4
3: /home/jovin/source/myProgram-build-desktop-Qt_4_8_1_in_Pfad__System__Release/myProgram



120 bytes in 1 blocks are definitely lost in loss record 6,202 of 6,918
in QFontDatabase::load(QFontPrivate const*, int) in /build/buildd/qt4-x11-4.8.1/src/gui/text/qfontdatabase.cpp:1062
1: operator new(unsigned int) in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so
2: QFontDatabase::load(QFontPrivate const*, int) in /build/buildd/qt4-x11-4.8.1/src/gui/text/qfontdatabase.cpp:1062
3: /home/jovin/source/myProgram-build-desktop-Qt_4_8_1_in_Pfad__System__Release/myProgram

jovin
20th May 2012, 12:08
Well, I more or less stopped the increase in memory. Now I create my stackScene only once and reuse it.

Result: The memory usage is at 4MB when the program is first started.
After switching to my QGraphicsScene it goes to 10MB and stays. Not matter how often I switch views. That's good.

Now I would like to give all memory free that's used for the QGraphicsScene (means go back to 4MB after leaving it).
How would I do that?

My QGraphicsScene is set up as mentioned in the opening post.
When deleting the stackScene and stackData pointer the memory doesn't really change.
I guess it's the engine that's so memory hungry, but when trying to delete the engine it throws a segmention fault.

I'm out of ideas.