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:
for (int i = 0; i < m_resultPoints->size(); ++i)
{
m_resultPoints
->append
( QPointF(m_resultPoints
->at
(m_resultPoints
->size
()-i
).
x() ,
(-1*m_resultPoints
->at
(m_resultPoints
->size
()-i
).
y() )));
}
for (int i = 0; i < m_resultPoints->size(); ++i)
{
m_resultPoints->append( QPointF(m_resultPoints->at(m_resultPoints->size()-i).x() , (-1*m_resultPoints->at(m_resultPoints->size()-i).y() )));
}
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.
Bookmarks