Important: Please read the Qt Code of Conduct -

AssignToHTMLImageElement memory leak

  • I'm trying to build a simple QT+HTML+JS+OpenCV application, that just shows the video from webcam on the html doc (shown by QWebView).

    I've noticed that every call to assignToHTMLImageElement increases memory allocated by the app. I beleive that shouldn't be a normal behavior. What am I doing wrong?

    Here is the code:
    int main(int argc, char *argv[])

    QApplication a(argc, argv);

    MyClass w;;

    return a.exec();


    class MyClass : public QMainWindow

    <... some code ...>

    Ui::MyClassClass ui;

    PureCaptureQThread cap;


    class PureCaptureQThread: public QThread

    Q_PROPERTY(QImage image READ getImage);

    <... some code ...>

    QImage getImage();

    QImage m_image;


    void newFrame();


    MyClass::MyClass(QWidget *parent, Qt::WFlags flags)

    : QMainWindow(parent, flags)


    <... some code ...>

    ui.webView->page()->mainFrame()->addToJavaScriptWindowObject("cap", &cap);


    function loadImage() {

    function initialize() {

    <body >
    <img id="m_frame" width="640" height="480" />

    [edit: Added missing coding tags @ SGaist]

  • Thanks SGaist for improving the message.

  • I'm not sure how it works but shouldn't javascript's garbage collector handle this? I guess each time you assign an image, the image data gets cached into memory and eventually garbage collected if not used. Maybe try to manually invoke the GC, but beware of any (bad) consequences.

  • Thanks, I'll try right now.

  • T3STY,

    Would you be so kind and point me to any doc on how to force GC? I've googled this, but foung nothing.


  • Hmm... fat chance...
    Seems like javascript's GC cannot be manually triggered. Although, if you remove all references to an object, that object's data is going to be GC-ed and freed. Unfortunately, it doesn't seem to be your case...

  • ok, thanks in any way

  • Noticed, that memory usage grows up to 1492 Mb only, then stops growing.

Log in to reply