PDA

View Full Version : QWebPage memory leakege.



bztd
21st June 2012, 10:38
Hello! I have found that QWebPage leaks memrory.
Here is my code:


#include <QApplication>
#include <QWebPage>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
while(1){
QWebPage* page = new QWebPage();
delete page;
}
return app.exec();
}

It leaks a lot of memory.
Can anyone please exmplain what I do wrong? Or may be this is a bug of WebKit?
I'm using Qt Creator 2.4.1 under Windows 7.

wysota
21st June 2012, 11:02
How do you know you have a leak here? How did you check that?

bztd
21st June 2012, 12:31
How do you know you have a leak here? How did you check that?

I'm just starting process manager and watch for memory usage of this process. I can see that usage of memory grows up 300-500kb per second.
I'm using debug version of project, version of QT libs is 4.8.1.0 and QTWebKit 4.9.0.0

wysota
21st June 2012, 14:22
Process manager does not report memory leaks.

bztd
22nd June 2012, 08:23
Process manager does not report memory leaks.

Well, in this case it looks like you can't help me. Will hope specialists from qt.nokia.com can.
Thank you for your reply and good luck with TrollTech.

wysota
22nd June 2012, 09:31
"Specialists from qt.nokia.com" will tell you the same thing. Process manager does not report memory usage correctly since it depends on how the system memory allocator works and the allocator usually doesn't deallocate memory from a process upon calling free/delete since memory allocation is an expensive process so it's deferred hoping that the process will want to reclaim the memory. If you suspect you have a memory leak then use a tool dedicated to looking for memory leaks.

ChrisW67
22nd June 2012, 09:31
Oh dear. What a strange response.

If you are going to make the extraordinary claim that something used widely leaks memory on a grand scale then you will need extraordinary evidence. Windows task manager does not detect memory leaks: and the high water mark it does report will be distorted by the inability of housekeeping task to compete with a process in a tight loop. You will need to use a tool that does detect memory leaks to gather your extraordinary evidence... even for the "specialists from qt.nokia.com" (not that Nokia is the author of QtWebKit (http://trac.webkit.org/wiki/QtWebKit)).

Valgrind is an option on UNIX-like systems and there are all sorts of options for Windows (http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows)

bztd
22nd June 2012, 14:36
Well, looks like work around for deleting pages ( and all other QObjects) is to run exec and do it in some slot by event..

TimShnaider
9th July 2012, 09:52
I'm having similar issues - what exactly did you do to overcome this?

amleto
9th July 2012, 12:47
similar issues being non-existent memory leak(s)?

d_stranz
9th July 2012, 19:23
I think that the OP has a basic misunderstanding of how heap memory management works. The "delete" operator does not do anything more than execute the destructor for the object instance being deleted, make the pointer to the memory used by that instance invalid, and tell the run-time memory manager that the memory in question can be reused if needed. But the tight loop is asking for new memory so fast, the memory manager can't do anything to optimize.

Putting millions of new() and delete() calls in a tight loop is like filling a bucket with water, emptying it, filling it again, emptying it, and doing it all so quickly that even if the drain plug is not in the bathtub, the bathtub eventually overflows because it can't hold any more water or drain it away fast enough.

If the OP wants proof that there is no memory leak, put a limit on the while() loop and let the program eventually exit (without having to kill it through the debugger). Let it run the loop 100 or 10000 times, then see if the debugger reports any memory leaks when the program exits normally.

amleto
9th July 2012, 20:45
Thread was hijacked - I dont think the OP is around any more.

d_stranz
9th July 2012, 21:23
Yes, you're probably right - didn't notice the big gap in dates. This forum should have the option to automatically close a thread to further replies after some period of time. Having an archival record is a good thing; being able to resurrect a years-old post by hijacking it with a new reply isn't.

The OP has probably gone off to consult with the experts at qt.nokia.com. No sense trying to get a decent answer from the monkeys here.

amleto
9th July 2012, 22:00
ooh ooh ah ah

TimShnaider
10th July 2012, 02:13
Ok settle down.

My scenario was quite a bit more complicated - I'm using QWebPage wrapped by a QObject utilised in QtScript, and I was getting all sorts of leaks and crashes when doing looped creations via QtScript prototype wrappers (e..g BBADBEEF)
Eventually after putting in QScriptEngine::collectGarbage calls and doing QApplication::processEvents and QScriptEngine::setProcessEventsInterval memory is now released and I can run scripts with large loops and memory usage is pretty much static.

But get rid of the 1st two and it was leaking like a sieve and crashing randomly, I think that was also a timing thing with object destruction and the event loop being processed etc. I don't have the time to understand it all right now, it was painful enough already.

I might revert the changes at some point and post the results and code snippets to replicate the problem here if anyone is interested in commenting.

wysota
10th July 2012, 05:27
Please don't abuse the term "memory leak". If memory leaks, it means it can't be reclaimed by the system memory allocator. If you just don't allow the framework you're using to release the memory because of a tight loop, then there is no leak, you just have an incorrect design in your application that causes unstable memory use.