Results 1 to 20 of 27

Thread: How to correctly re-assign a pointer?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Question How to correctly re-assign a pointer?

    Hello!

    I started another topic yesterday, but I guess it went far into detail.
    My questions comes down to this:

    How can I correctly re-assign a pointer?

    In my case, I have a QtDesigner-Layout, which creates a QTableView:

    Qt Code:
    1. public:
    2. QTableView *tableView;
    3. ...
    4. void setupUi(QDialog *AddressBook)
    5. {
    6. tableView = new QTableView(AddressBook);
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    In the class which uses this layout I want to pass a pointer to a pre-defined QTableView to the one created by the layout.

    AddressBook-class:
    Qt Code:
    1. AddressBook::AddressBook(DataManager* dmgr, QWidget *parent): QDialog(parent)
    2. {
    3. ...
    4. tableView = dmgr->customerTableView();
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 

    DataManager-class

    Qt Code:
    1. ...
    2. QTableView *m_customerTableView;
    3. QTableView *customerTableView();
    4. ...
    5. m_customerTableView = new QTableView;
    6. ...
    7. QTableView *DataManager::customerTableView()
    8. {
    9. return m_customerTableView;
    10. }
    11. ...
    To copy to clipboard, switch view to plain text mode 

    I guess to avoid memory leaks I have to delete the original tableView created by QtDesigner.

    I tried the following but could get it running, since I am not so familiar with pointers yet. (coming from Java) So there could be a misunderstanding on how pointers work.

    In AddressBook-class:

    Qt Code:
    1. delete tableView;
    2. QTableView *tableView = dmgr->customerTableView();
    To copy to clipboard, switch view to plain text mode 

    I hope you can help me.

    Kind regards,
    HomeR

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to correctly re-assign a pointer?

    You are shadowing your member pointer.
    There is no need to redeclare it:
    Qt Code:
    1. delete tableView;
    2. /*QTableView * */ tableView = dmgr->customerTableView();
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: How to correctly re-assign a pointer?

    Thanks for you anwser.
    I tried what you said, but it's still not working.

    Does shadowing mean, that a global variable is hidden because a local variable is used instead?

    What exactly happens here?
    Qt Code:
    1. delete tableView;
    2. /*QTableView * */ tableView = dmgr->customerTableView();
    To copy to clipboard, switch view to plain text mode 

    As far as I unterstand, delete tableView does remove the QTableView object created in the QtDesigner-file ui_addressbook.h :

    Qt Code:
    1. class Ui_AddressBook
    2. {
    3. public:
    4. QTableView *tableView;
    5. ..
    6. }
    To copy to clipboard, switch view to plain text mode 

    Then my prepared QTableView returned by dmgr->customerTableView(); is assigned - but assigned to what? There is no other variable named tableView existing, I guess. Do I have to define an additional one with the same name in
    AddressBook.h ?

    Right now my code is

    Qt Code:
    1. delete tableView;
    2. Q_CHECK_PTR(tableView);
    3. Q_CHECK_PTR(dmgr->customerTableView());
    4. tableView = dmgr->customerTableView();
    To copy to clipboard, switch view to plain text mode 

    Why does die application not quit on "Q_CHECK_PTR(tableView)" after "delete tableView" ?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to correctly re-assign a pointer?

    I tried what you said, but it's still not working.
    please define what 'not working' mean here.

    Does shadowing mean, that a global variable is hidden because a local variable is used instead?
    Yes, but instead of 'global' here its in the class scope. (so its only visible in the scope of the class).

    What exactly happens here?
    Well, unless there is more code that I havn't seen then:
    In the very first code segment you posted in the firt post, you declare
    Qt Code:
    1. QTableView *tableView
    To copy to clipboard, switch view to plain text mode 
    As a class member.
    So if this code:
    Qt Code:
    1. delete tableView;
    2. /*QTableView * */ tableView = dmgr->customerTableView();
    To copy to clipboard, switch view to plain text mode 
    Is in the same clase, then tableViewe is already defined, and defining it again in the method, would shadow the one defined in the class header, and the address will be assigned to the local shadow, which gets destroyed when the method exits.
    Note - the POINTER gets destroyed, but the address allocated still lives - and this called a dangling pointer - which is a memory leak - since you can't get it and delete it any more. (gets deleted when the memory of the application is returned)

    Do I have to define an additional one with the same name in
    AddressBook.h
    No, and if you would, you would get a complication error.


    As far as I unterstand, delete tableView does remove the QTableView object created in the QtDesigner-file ui_addressbook.h :
    Yes and no.
    'delete' deletes the object pointed to by the poiter (calling its destructor etc).
    In your case, its ture, that the first allocation is done in the ui file.

    Then my prepared QTableView returned by dmgr->customerTableView(); is assigned - but assigned to what? There is no other variable named tableView existing, I guess.
    Don't guess - be sure!

    Why does die application not quit on "Q_CHECK_PTR(tableView)" after "delete tableView" ?
    because delete does not set the deleted pointer to NULL.
    So you should do:
    delete tableView;
    Q_CHECK_PTR(tableView); //will go through, since tableViewer is not NULL (but filled with garbage)
    tableView = NULL;
    Q_CHECK_PTR(tableView); // this will assert!
    Q_CHECK_PTR(dmgr->customerTableView());
    tableView = dmgr->customerTableView();
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    homerun4711 (3rd January 2011)

Similar Threads

  1. Cannot assign to pixmap during painting
    By fanatos in forum Qt Programming
    Replies: 9
    Last Post: 22nd February 2016, 17:56
  2. Replies: 1
    Last Post: 4th December 2010, 17:20
  3. Assign a wchar_t array to QString
    By jacky in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 12:28
  4. boost::assign
    By Sivert in forum General Programming
    Replies: 0
    Last Post: 2nd May 2007, 00:23
  5. I can't assign TableView signals...
    By tomek in forum Newbie
    Replies: 5
    Last Post: 9th January 2006, 21:04

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.