Results 1 to 4 of 4

Thread: pointers and leaks

  1. #1
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default pointers and leaks

    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:

    Qt Code:
    1. void MyApp::printDialog()
    2. {
    3. QSortFilterProxyModel *tmMain = qobject_cast<QSortFilterProxyModel *>(cw->mainTable()->model());
    4. SortFilterProxyModel *tmDetail = qobject_cast<SortFilterProxyModel *>(cw->detailTable()->model());
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: pointers and leaks

    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:

    Qt Code:
    1. //Below is a local pointer pointing to new memory allocated on the heap, when the
    2. //method ends, the printModel pointer will go out of scope and be destroyed - that means
    3. //you will have no more access to the memory you allocated with new.
    4. //This is a memory leak.
    5.  
    6.  
    7.  
    8. //Below you create two local pointers (as above), but instantiate them with pointers to
    9. //memory which has already been allocated. When these pointers fall out of scope, it is
    10. //not a memory leak because you did not allocate any memory here in the first place.
    11. //You still have a pointer to the model memory, which is returned by model().
    12.  
    13. QSortFilterProxyModel * tmMain = qobject_cast<QSortFilterProxyModel*>(cw->mainTable()->model());
    14. QSortFilterProxyModel * tmDetail = qobject_cast<QSortFilterProxyModel*>(cw->detailTable()->model());
    To copy to clipboard, switch view to plain text mode 

    I'd be happy to help with any other questions you have. I know it can be a bit confusing.
    Last edited by JimDaniel; 11th October 2008 at 16:33.

  3. The following user says thank you to JimDaniel for this useful post:

    janus (11th October 2008)

  4. #3
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: pointers and leaks

    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?

  5. #4
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: pointers and leaks

    Exactly correct.

  6. The following 2 users say thank you to JimDaniel for this useful post:

    gustavosbarreto (16th October 2008), janus (11th October 2008)

Similar Threads

  1. Memory leaks in QT apps
    By sar_van81 in forum Qt Programming
    Replies: 7
    Last Post: 19th July 2008, 08:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.