[Qt-interest] Will it cause a memory leak?

Donal O'Connor donaloconnor at gmail.com
Thu Jul 9 12:55:10 CEST 2009


No, I would say not. From the docs:

void QApplication::postEvent (
QObject<http://doc.trolltech.com/3.3/qobject.html> * receiver,
QEvent <http://doc.trolltech.com/3.3/qevent.html> * event ) [static]

Note: This function is
thread-safe<http://doc.trolltech.com/3.3/threads.html#threadsafe>when
Qt is built withthread support.
Adds the event *event* with the object *receiver* as the receiver of the
event, to an event queue and returns immediately.

*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. *

When control returns to the main event loop, all events that are stored in
the queue will be sent using the
notify<http://doc.trolltech.com/3.3/qapplication.html#notify>()
function.


On Thu, Jul 9, 2009 at 11:44 AM, Kermit Mei <kermit.mei at gmail.com> wrote:

>
> Hello community!
>
> I want to pass an event to another widget, but I doubt the following
> code would cause a memory leak:
>
> void HtmlEditor::fileNew()
> {
>    if (maybeSave()) {
>        ui->webView->setHtml("<p></p>");
>        ui->webView->setFocus();
>        ui->webView->page()->setContentEditable(true);
>        setCurrentFileName(QString());
>        setWindowModified(false);
>
>        // quirk in QWebView: need an initial mouse click to show the cursor
>
>        int mx = ui->webView->width() / 2;
>
>        int my = ui->webView->height() / 2;
>
>        QPoint center = QPoint(mx, my);
>
>        QMouseEvent *e1 = new QMouseEvent(QEvent::MouseButtonPress, center,
>                                          Qt::LeftButton, Qt::LeftButton,
>                                          Qt::NoModifier);
>
>        QMouseEvent *e2 = new QMouseEvent(QEvent::MouseButtonRelease,
> center,
>                                          Qt::LeftButton, Qt::LeftButton,
>                                          Qt::NoModifier);
>        QApplication::postEvent(ui->webView, e1);
>
>        QApplication::postEvent(ui->webView, e2);
>    }
> }
>
>
> The tow QMouseEvent haven't been delete in anywhere. If it is really a
> memory leak, then where can I delete them?
>
> Thanks.
> Kermit
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090709/ff1735bd/attachment.html 


More information about the Qt-interest-old mailing list