Results 1 to 2 of 2

Thread: Storing QList QLinkedList Iterators for automatic deletion

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Storing QList QLinkedList Iterators for automatic deletion

    Hi everyone,

    I have a serious question about the usage of Iterators in Qt. I want to have a resource manager for some project and I thought the following data structure would be great:

    Each resource should be in memory only once. Whenever a resource is requested, we just get a data pointer to this resource. The data pointer will automatically register itself to the resource, so that we can find out, how often it was used and also WHERE it was used. The "WHERE" is the main difference to only using shared or weak pointers. The great thing about this data structure is not only, that whenever the data itself becomes invalid, all data pointers become invalid (like with weak pointers), but also, that we can register a callback function to the data pointer that will be called on invalidation. So whenever the data itself is deleted it will go through its registered list of data pointers and call the registered callback function before the data pointer becomes invalid. By doing so, the program can directly react on the decision of the resource manager. Another thing is, that whenever a data pointer is deleted it has to unregister itself from the resource.

    Now here comes the problem: In the resource itself I just want to use a light weight collector for the data pointer registration like a list or a linked list. Please note, that we never need to access a single element directly by ID, so a linked list fits much better than a vector in this scenario. The only thing we want to do is calling all the callbacks of the registered daa pointers, so just iterating over the list in the resource in O(n). On the other hand, if just a data pointer becomes invalid (deleted), we have to unregister it from the resource. Since the resource might be used thousands of times, we do not want go through the whole list and search for the current deleted data pointer. Instead, we could store an iterator to the corresponding element in the list directly in the data pointer, whenever a new data pointer is created. So when a data pointer is deleted, we can just erase the stored iterator from that list in O(1).

    From the theory point of view, this should be no problem, because a linked list iterator should just point to an element and since it is linked, this element will not move in memory. The problem is now, that QList is internally implemented as some kind of std::deque, so storing an iterator to this data structure will cause problems, because if some element is deleted another iterator might point to some invalid memory sections. My question is now, if I am really right, when I say that an Iterator to a QLinkedList behaves completely different and remains stable, also if other elements from this linked list are removed.

    I am sorry, that I could not create a smaller example, but at least I wrote it in one file for simplicity of testing. The code demonstrates the general idea. You can try the different data structures by changing the MyList define to QList (will crash because of instable iterators) or QLinkedList (will not crash because of stable iterators- if I am right and this is not only an unhappy coincidence).

    I'm looking forward for your great answers.

    Greetz,
    N3O
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Storing QList QLinkedList Iterators for automatic deletion

    Hi again,

    I might post the essential problem in a short code snippet. Is the following code guaranteed to evaluate right for MyList = QLinkedList?

    Qt Code:
    1. #include <QtCore/QList>
    2. #include <QtCore/QLinkedList>
    3. #include <QtCore/QString>
    4. #include <QtCore/QDebug>
    5.  
    6. #define MyList QLinkedList
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. MyList<QString> qList;
    11. qList.append("One");
    12. MyList<QString>::iterator qListElement1 = qList.end() - 1;
    13. qList.append("Two");
    14. MyList<QString>::iterator qListElement2 = qList.end() - 1;
    15. qList.append("Three");
    16. MyList<QString>::iterator qListElement3 = qList.end() - 1;
    17. qList.append("Four");
    18. MyList<QString>::iterator qListElement4 = qList.end() - 1;
    19.  
    20. //This will crash for QList, but not for QLinkedList
    21. qList.erase(qListElement2);
    22.  
    23. for(MyList<QString>::iterator i = qList.begin();
    24. i != qList.end();
    25. ++i)
    26. {
    27. qDebug() << *i;
    28. }
    29.  
    30. return 0;
    31. }
    To copy to clipboard, switch view to plain text mode 

    I assume that it does not work with QList, because it is somehow implemented like a kind of std::deque, which means it allocates a whole memory block. When deleting an element in such a data structure, other Iterators might point to some illegal position in memory. But: Can I be sure that this is not the case with a QLinkedList, because each element always remains at the same position in memory, no matter if new elements are inserted or some elements are deleted?

    Greetz,
    N3O

Similar Threads

  1. QVector, QLinkedList
    By aash_89 in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2010, 02:20
  2. qDeleteAll() and linked list iterators
    By jkyle in forum Qt Programming
    Replies: 2
    Last Post: 29th June 2010, 22:18
  3. Replies: 10
    Last Post: 20th June 2010, 22:29
  4. QList or QLinkedList
    By eleanor in forum Newbie
    Replies: 6
    Last Post: 9th November 2007, 09:40
  5. QLinkedList and iterators
    By eu.x in forum Newbie
    Replies: 1
    Last Post: 19th April 2007, 19:38

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.