QTBUG-88248 存在于 Qt-5.15 初期版本中,Qt-5.15.6 声称已经解决了这个
问题。
最近一年多主要使用由论坛里 @
fsu0413(感谢大佬的贡献) 发布的 Qt5.15.*-Windows-x86_64-VS2019 版本,
但包括最近发布的 Qt-5.15.9 版本,
似乎 QTBUG-88248 这个问题还是存在,使用的测试代码是在 QTBUG-87774 的代码的基础上修改的。
PID 13768 用的是自
编译的 Qt-5.9.9-Windows-x86_64-VS2019-16.11.26,这个版本测试期间内存占用很稳定,
PID 1220 用的是
fsu0413 最近发布的 Qt5.15.9-Windows-x86_64-VS2019-16.11.26-20230507.7z :

因此不确定 Qt 针对 Qt-5.15 开源的代码中是否有针对 QTBUG-88248 的修复补丁,
求助论坛里有使用 Qt-5.15 版本(特别是使用商业闭源版本)或参与 Qt
开发的朋友帮助确认一下这个问题的是否依然存在。
测试代码:
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
- class QToolBar;
- class QPushButton;
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
- public slots:
- void refresh_toolbar();
- void update_count( bool del = false );
- void update_count_and_delete();
- private:
- Ui::MainWindow *ui;
- QToolBar *tb;
- QPushButton * pb;
- };
- #endif // MAINWINDOW_H
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QToolBar>
- #include <QTimer>
- #include <QPushButton>
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow),
- tb( nullptr ),
- pb( new QPushButton(this) )
- {
- ui->setupUi(this);
- setCentralWidget(pb);
- pb->setText(QLatin1String("Push me to start the test"));
- connect(pb, SIGNAL(clicked(bool)), this, SLOT(refresh_toolbar()));
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::update_count( bool del )
- {
- pb->setText( tr("Actions : %1").arg( tb->children().count() ) );
- if( del )
- {
- removeToolBar( tb );
- tb->deleteLater();
- tb = nullptr;
- }
- }
- void MainWindow::update_count_and_delete()
- {
- update_count(true);
- }
- void MainWindow::refresh_toolbar()
- {
- static int gg = 0;
- if( tb )
- {
- // This would release those actions and free the memory,
- // Qt-5.9.9s build with mingw32 and msvc-2019 do that OK,
- // Qt-5.12.9-msvc2019 and Qt-5.15.1-msvc2019 leaks memory
- QList<QAction*> acts = tb->actions();
- foreach (QAction* act, acts) {
- tb->removeAction(act);
- delete act;
- }
- }
- else
- {
- tb = new QToolBar(this);
- addToolBar( tb );
- update_count();
- }
- // Here actions's memory may not be released!
- tb->clear();
- if( gg++ < 1000 )
- {
- for(int i = 0; i < 100; ++i)
- {
- int title = gg * 1000 + i;
- tb->addAction(QString::number(title));
- }
- QTimer::singleShot(10, this, SLOT(refresh_toolbar()));
- }
- else
- {
- // remove and delete the toolbar
- QTimer::singleShot(10, this, SLOT(update_count_and_delete()));
- gg = 0;
- }
- }