2

Qt/Qml (Qml は QWidget に埋め込まれています) でアプリケーションを開発しています。QQuickView (埋め込み) を含む QWidget を削除すると、割り当てられたメモリは完全には解放されません。

アプリケーションに QWidget を追加すると、約 30MB のメモリが割り当てられますが、ウィジェットが削除されると、約 20MB のメモリしか解放されません。

QWidget のデストラクタで、QQuickView インスタンスを削除しましたが、他に大きなオブジェクトはありません。

また、QQuickView がメモリを適切に解放しないことは確かです。

QQuickView によって割り当てられたメモリ全体を解放するにはどうすればよいですか?

注: コードは非常に大きい (160000 行) ため、サンプル コードを配置できません。

前もって感謝します...

4

1 に答える 1

5

の作成と削除に実際のリークがあるかどうかを判断する簡単なテストを作成しましたQQUickWidget

class Widget : public QWidget {
    Q_OBJECT
public:
    Widget(QWidget *parent = 0) : QWidget(parent) {
        widget = 0;
        count = 0;
        resize(200, 200);
        layout = new QVBoxLayout(this);
        setLayout(layout);
        QTimer * t = new QTimer(this);
        t->setInterval(200);
        t->setSingleShot(false);
        t->start();
        connect (t, SIGNAL(timeout()), this, SLOT(toggleQuickView()));
    }

public slots:
    void toggleQuickView() {
        if (!widget) {
            widget = new QQuickWidget;
            widget->setSource(QUrl::fromLocalFile("d:\\main.qml"));
            connect(widget, SIGNAL(destroyed()), this, SLOT(echo()));
            layout->addWidget(widget);
        } else {
            layout->removeWidget(widget);
            widget->deleteLater();
            widget = 0;
        }
    }

    void echo() {
        PROCESS_MEMORY_COUNTERS memcount;
        if (!GetProcessMemoryInfo(GetCurrentProcess(), &memcount, sizeof(memcount))) return;
        qDebug() << ++count << "created and destroyed," << memcount.WorkingSetSize / (1024 * 1024) << "MB memory used";
    }

private:
    QVBoxLayout * layout;
    QQuickWidget * widget;
    int count;
};

QQuickWidget内部にロードされた QML ファイルを作成/破棄するタイマーがあり、結果は最初は増加しますが、メモリ使用量は時間とともに安定し、Qt コードでメモリ リークが発生した可能性が低いことを示しています。実際にメモリ リークが発生します。障害は Qt ではなく、独自のコードにあります。

また、タスク マネージャーが実際に よりも少ないメモリを使用してプロセスを示したことは言及する価値がありますGetProcessMemoryInfo()。タスク マネージャーの読み取り値も、値がさらに変動しているものの、メモリ リークを示していませんでした。

出力は次のとおりです。

1 created and destroyed, 41 MB memory used
2 created and destroyed, 44 MB memory used
3 created and destroyed, 44 MB memory used
4 created and destroyed, 48 MB memory used
5 created and destroyed, 48 MB memory used
6 created and destroyed, 48 MB memory used
7 created and destroyed, 48 MB memory used
8 created and destroyed, 48 MB memory used
9 created and destroyed, 48 MB memory used
10 created and destroyed, 48 MB memory used
11 created and destroyed, 52 MB memory used
12 created and destroyed, 52 MB memory used
13 created and destroyed, 52 MB memory used
14 created and destroyed, 52 MB memory used
15 created and destroyed, 52 MB memory used
16 created and destroyed, 52 MB memory used
17 created and destroyed, 52 MB memory used
18 created and destroyed, 52 MB memory used
19 created and destroyed, 52 MB memory used
20 created and destroyed, 52 MB memory used
21 created and destroyed, 53 MB memory used
...
50 created and destroyed, 53 MB memory used
...
100 created and destroyed, 53 MB memory used
...
200 created and destroyed, 53 MB memory used
...
500 created and destroyed, 53 MB memory used
于 2014-11-29T15:32:42.267 に答える