Results 1 to 4 of 4

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

  1. #1
    Join Date
    Sep 2017
    Posts
    22
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

    I have a QList<QPOintf> that has 10 element inside it, i want to copy the list in a reverse order then append it again to the list but this is done after changing the y value of QPointf. For example:

    suppose our list is as follow:
    Qt Code:
    1. m_resultPoints has the following points [(1,2),(1,3),(5,6),(7,2),(3,4),(7,9),(4,5),(2,0),(2,4),(4,7)];
    To copy to clipboard, switch view to plain text mode 
    I want it to be:
    Qt Code:
    1. m_resultPoints should have the following points [(1,2),(1,3),(5,6),(7,2),(3,4),(7,9),(4,5),(2,0),(2,4),(4,7),(4,-7),(2,-4),(2,-0),(4,-5),(7,-9),(3,-4),(7,-2),(5,-6),(1,-3),(1,-2)];
    To copy to clipboard, switch view to plain text mode 
    So I put the list in a loop and created a QPointf dynamically using new operator and appended it to the list content. Now who take the owner ship of the points, i think it is the QList, am i right ?
    I also wonder if there is another way to do this in more elegant way, thanks in advance.

    Here is the code snippet that is used to do the appending and the reversing of the list points:
    Qt Code:
    1. for (int i = 0; i < m_resultPoints->size(); ++i) {
    2. m_resultPoints->append(*new QPointF(m_resultPoints->at(m_resultPoints->size()-i).x() , (-1*m_resultPoints->at(m_resultPoints->size()-i).y() )));
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Ahmed Abdellatif; 14th September 2018 at 15:23.

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    405
    Thanked 51 Times in 47 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    Why do you create the QPointF on the heap instead just passing the QPointF to the QVector - then you don't have to clean up the memory when removing the points from the vector. And it's faster too...

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

    Ahmed Abdellatif (14th September 2018)

  4. #3
    Join Date
    Jul 2012
    Posts
    188
    Thanks
    9
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    Literally everything about this is wrong. You need to learn C++ first.


    * if you do '*new QPointf...' you are leaking memory. You first create a QPointof on the heap, then deference it with *. This creates a copy of the allocated QPointf.

    * Dont allocate it on the heap with new at all. Just do QPointf(1,1).

    * resultPoints->size() is the current size, if you append to the list, the size grows. Hence the loop will go on forever.

    Without having tried it, more like so:

    QList<QPointF> m_resultPoints;
    int size = m_resultPoints.size();
    for(int i=0; i<size; i++)
    {
    points.append( QPointF(m_resultPoints->at(size-i).x() , (-1*m_resultPoints->at(size-i).y() )
    }

  5. The following 2 users say thank you to tuli for this useful post:

    Ahmed Abdellatif (14th September 2018), d_stranz (14th September 2018)

  6. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,812
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    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 18: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.

  7. 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, 14:26
  2. Using QPointF in QShareData class
    By SasaVilic in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2010, 01:33
  3. QPointF == operator usage
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2010, 09:41
  4. QPointF equality operator
    By jenova_project in forum Newbie
    Replies: 1
    Last Post: 22nd September 2008, 17:42
  5. QPointF transformation
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2008, 11: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.