Thread: QT question

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    QT question

    In the examples of QT, address book, I see this code in the AddressBook class:
    Code:
        
    //in class
    AddressBook* ui;
    QLineEdit* nameLine;
    
    //in constructor
    nameLine = new QLineEdit;
    nameLine = ui->nameLine;
    So my question is since nameLine points to a new object QLineEdit and then points to ui->nameLine, isn't a memory leak created?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by C_ntua View Post
    In the examples of QT, address book, I see this code in the AddressBook class:
    Code:
        
    //in class
    AddressBook* ui;
    QLineEdit* nameLine;
    
    //in constructor
    nameLine = new QLineEdit;
    nameLine = ui->nameLine;
    So my question is since nameLine points to a new object QLineEdit and then points to ui->nameLine, isn't a memory leak created?
    Is ui pointing to this? If so, it's just a pointless circuitous reference, not a memory leak. If not, it's definitely a memory leak.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The constructor starts
    Code:
    AddressBook::AddressBook(QWidget *parent)
        : QWidget(parent), ui(new Ui::AddressBook)
    {
         ui->setupUi(this);
    Anyway, I think it is kind of meaningless to see what QTCreator actually does.
    The point is that there seems to be ne meaning for all of this, except if the example was taken from an older versiont of QT, which I believe is the case.
    The whole point was that a basic object is created that is pointed by ui and we then can only play around with this. So we just link the pointer of this to the objects of the basic object pointed by ui.

    If I don't allocate memory it seems to work fine.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If ui->nameLine is not the same object as nameLine (and it seems to be the case) then it's definitely a leak, yeah.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I am quite sure it is a leak. And there is nothing that deletes the object in the destructor. But the thing is why would you ever do
    Code:
    pointer = new Obj;
    pointer = something;
    I mean, no matter what the case is, how could the above code possibly not to something weird?

    They actually have two examples in there. The example I am using is the correct one, the other is how to create widgets without not graphically, but purely code-wise.

    I really don't know why do they have this line of code...

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    qt's memory management is utter fail. it at least encourages, if not outright requires, bad practices.

    that first (leaking) line would normally create a qwidget in it's own window. if you were to close the window, it would clean up after itself.

    i don't understand the purpose of phrasing the code in that manner in the slightest either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. Any QT users out there - on a Mac?
    By Dino in forum C++ Programming
    Replies: 9
    Last Post: 09-29-2009, 07:00 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. C & QT Question
    By DaveHope in forum Linux Programming
    Replies: 4
    Last Post: 12-22-2003, 01:32 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM