PDA

View Full Version : pointers and leaks



janus
11th October 2008, 11:10
Hi,

i do not fully understand the concept of pointers. Maybe someone could help me to understand.

I have a printing function that is called several times. The data within the models that are shown in the centralWidgets tables are printed. Therefore I am doing a cast to get the proxymodels:


void MyApp::printDialog()
{
QStandardItemModel *printModel = new QStandardItemModel;
QSortFilterProxyModel *tmMain = qobject_cast<QSortFilterProxyModel *>(cw->mainTable()->model());
SortFilterProxyModel *tmDetail = qobject_cast<SortFilterProxyModel *>(cw->detailTable()->model());
....
}


I am processing the data and insert them into the printingModel. My question is: What happens to the pointers *tmMain and *tmDetail after they run out of scope? Are they deleted because they are local variables? Or do I need to set them to =0? Do I get a copy of the object from the cast or a pointer to the object ...? Sorry, many questions. Thx in advance.

JimDaniel
11th October 2008, 16:19
The only memory you need to delete here is the new QStandardItemModel.

There may be some confusion here as to what a pointer is. A pointer is just a basic variable, like a int or a float. When you call delete on a pointer, you are not deleting the pointer itself, you are deleting the memory the pointer is pointing to. So you tell me, would you want to delete the model memory here? That would probably be bad for your program.

Look at your qobject_cast - that will tell you what you are getting back, in this case a pointer to a QSortFilterProxyModel. So what you have is this:



//Below is a local pointer pointing to new memory allocated on the heap, when the
//method ends, the printModel pointer will go out of scope and be destroyed - that means
//you will have no more access to the memory you allocated with new.
//This is a memory leak.

QStandardItemModel * printModel = new QStandardItemModel;


//Below you create two local pointers (as above), but instantiate them with pointers to
//memory which has already been allocated. When these pointers fall out of scope, it is
//not a memory leak because you did not allocate any memory here in the first place.
//You still have a pointer to the model memory, which is returned by model().

QSortFilterProxyModel * tmMain = qobject_cast<QSortFilterProxyModel*>(cw->mainTable()->model());
QSortFilterProxyModel * tmDetail = qobject_cast<QSortFilterProxyModel*>(cw->detailTable()->model());



I'd be happy to help with any other questions you have. I know it can be a bit confusing.

janus
11th October 2008, 18:52
Hi,

thx a lot JimDaniel. So what I did is, that I delete the printModel at the end of the function. That's correct as far as I understood your explanation. I can not delete the other pointers because i still need the objects after finishing the function.
Since the pointers *tmMain and *tmDetail are variables too (as you wrote), I suppose they (the pointers) are created on the stack and therefore deleted after the function is finished?
So I have pointers on the stack pointing to memory (objects) on the heap? I that correct? :o

JimDaniel
11th October 2008, 21:26
Exactly correct.