PDA

View Full Version : QDrag : memory allocation



kghose
14th August 2008, 22:55
I'm using Instruments (on Mac) to track memory leaks in a QT app I wrote. I've gotten rid of the ones due to my code and am left with those it is not clear are really leaks and are from QT.

1. QMessageBox - is it destroyed on close?
2. QDrag - is it destroyed when the drag is complete?

An example of the code that registers as a leak is:

void about() {
QMessageBox::information(this, "About X", "X is a program.\n")
}

and,


void PhotoMailer::mousePressEvent(QMouseEvent *event) {
//http://doc.trolltech.com/4.2/dnd.html
if (event->button() == Qt::LeftButton) {
QDrag *drag = new QDrag((QWidget*)this);
QMimeData *mimeData = new QMimeData;
mimeData->setUrls(url);
drag->setMimeData(mimeData);
drag->start();
}
}

My question is, should I worry - they are created with new and then 'lost', i.e. the calling function returns without telling anyone of the pointer, so I understand that a profiler will flag this as a leak, but will QT take care of them? Or have I missed a programming paradigm here.

Thanks!
-K

wysota
14th August 2008, 23:57
1. QMessageBox - is it destroyed on close?
If used as a static call as in the code you presented then yes.

2. QDrag - is it destroyed when the drag is complete?
Yes.



My question is, should I worry - they are created with new and then 'lost'
The message box is not created with new, at least not explicitely. I'd say it's created on stack and goes out of scope when the function returns.


, i.e. the calling function returns without telling anyone of the pointer, so I understand that a profiler will flag this as a leak, but will QT take care of them? Or have I missed a programming paradigm here.

As far as I remember the docs say that Qt takes ownership of the drag object.