gpt4 book ai didi

c++ - 以下代码中嵌入的 "new"是否会导致内存泄漏?

转载 作者:太空宇宙 更新时间:2023-11-04 14:46:59 25 4
gpt4 key购买 nike

我不这么认为,我认为内存在函数终止时被删除,但我想与社区核实。

void MainWindow::editCopy   (void)
{
QWidget *pqwgtFocus = QApplication::focusWidget();

if (pqwgtFocus != 0)
{
QApplication::postEvent ( pqwgtFocus,
new QKeyEvent ( QEvent::KeyPress,
Qt::Key_C,
Qt::ControlModifier
)
);

QApplication::postEvent ( pqwgtFocus,
new QKeyEvent ( QEvent::KeyRelease,
Qt::Key_C,
Qt::ControlModifier
)
);
}

return;
}

最佳答案

是的,该应用程序将取得所有权并且它非常安全。

The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted. It is not safe to access the event after it has been posted.

https://doc.qt.io/qt-5/qcoreapplication.html#postEvent

关于c++ - 以下代码中嵌入的 "new"是否会导致内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54655703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com