Results 1 to 4 of 4

Thread: Ownership of the QPointf that are created with new appended in QList<QPointf> list

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Ownership of the QPointf that are created with new appended in QList<QPointf> lis

    Now who take the owner ship of the points, i think it is the QList, am i right ?
    No, the list does not take ownership. QList is not a QObject-based class, so there is no parent-child or other type of ownership relationship.

    I think you also misunderstand the difference between QList< QPointF > and QList< QPointF * >. As a result your code has a memory leak.

    You create QPointF instances on the heap using operator new(), and then pass a reference (*pointf) into the QList, which then makes a copy of the QPointF's contents. The QPointF pointer you created becomes an orphan pointer - the memory is still allocated, but you have no way to free it because the pointer to it is only accessible for the life of the append call. So every time you call append, a little bit more heap gets eaten away and is never available again until your program exits.

    The way around this is to not create the pointer in the first place, as Christian says. Your code should be changed to this:

    Qt Code:
    1. for (int i = 0; i < m_resultPoints->size(); ++i)
    2. {
    3. m_resultPoints->append( QPointF(m_resultPoints->at(m_resultPoints->size()-i).x() , (-1*m_resultPoints->at(m_resultPoints->size()-i).y() )));
    4. }
    To copy to clipboard, switch view to plain text mode 

    Look carefully at the argument to the append() call and you will see the difference. In this case, the QPointF instances are being allocated on the stack (not the heap) and will automatically be destroyed when the QList is destroyed.

    Edit: I see tuli and I posted at the same time. His comments are correct, too. I hadn't noticed that the resultPoints->size() call was inside the loop, and it will result in worse than just a memory leak. It will cause your program to run in an infinite loop until it crashes because it has run out of memory. If you are running on a 64-bit computer, it will eventually bring your computer to a halt too as the program keeps trying to allocate more scratch memory from the disk-based heap.
    Last edited by d_stranz; 14th September 2018 at 17:27.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    Ahmed Abdellatif (14th September 2018)

Similar Threads

  1. QPointF - draw only 30% of window
    By pajczur in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2017, 13:26
  2. Using QPointF in QShareData class
    By SasaVilic in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2010, 00:33
  3. QPointF == operator usage
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2010, 08:41
  4. QPointF equality operator
    By jenova_project in forum Newbie
    Replies: 1
    Last Post: 22nd September 2008, 16:42
  5. QPointF transformation
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2008, 10:11

Tags for this Thread

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.