簡體   English   中英

Qt 遞歸單次定時器似乎導致 memory 泄漏

[英]Qt recursive singleshot timer seems to cause memory leak

我在 C++/Qt 中有一個 function ,它使用單次計時器遞歸調用。 據我了解,單次計時器應該超時一次,並且不需要任何 memory 清理。 我已將 memory 泄漏縮小到僅一行代碼。 隨着時間的推移,它會導致程序緩慢但穩定地使用更多 RAM。 我讓它運行了兩個小時,它從大約 50 MB 開始增加到 2.2 GB。 作為一個最小的例子,我有這個:

void executionLoop() {
    QTimer::singleShot(1, Qt::PreciseTimer, this, SLOT(executionLoop()));
}

因此,如果這像我認為的那樣工作,executionLoop 應該運行,設置一個單次計時器,讓 Qt 做任何事情,然后在 1 毫秒后回調,設置另一個單次並重復直到程序停止。 不知何故,程序只是從這一行繼續吃 RAM。 如果我這樣做:

while (0 == 0)
{
    QCoreApplication::processevents();
    executionLoop();
}

當 executionLoop 為空 function 時,仍然存在 memory 泄漏,但它呈指數級增長。 有誰知道發生了什么?

step 1:
main calls executionloop which schedules timerA
step 2:
main calls executionloop which schedules timerB
timerA calls executionloop which schedules timerC
step 3:
main calls executionloop which schedules timerD
timerB calls executionloop which schedules timerE
timerC calls executionloop which schedules timerF
step 4:
...

我得到了我的答案。 這被確認為 Windows Qt 6.0.0 及更高版本中的錯誤。 它顯然不存在於舊版本中。 如果有人感興趣,將在此處跟蹤該錯誤: https://bugreports.qt.io/browse/QTBUG-92912

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM