I have the following structure on the software I am developing:
ClassA:
QHash<int, ClassB>
ClassB:
QHash<int, ClassC>
ClassC:
QMap<ID, QSharedPointer<ClassD> > (this is because I need to have the items ordered)
QHash<int, QSharedPointer<ClassD> > (this exists so I can access an item via id)
My question is if it is safe to have a pointer, that will be edited, to an element inside a data structure. I have been getting errors while trying to debug in which the debugger is unable to stop at a break point and I get a SIGTRAP error, but I am not sure if it is related to a memory issue on this.
To give a better example, related to the software I'm developing I have a QHash<int, Take>
that represents a list of videos. The user will be editing only one video at a time, so I have a pointer to the current video, which is a Take inside the Hash. Each Take has a bunch of parameters that can be edited but the most common is a QMap of Notes. Is is safe to do something like this?
Take *currentTake = &takes[currentTakeId];
----//---
currentTake->addNote(new Note());
currentTake->changeSomeParameter();
etc
Whether (or how long) it is safe to keep a pointer/reference to an element of a collection is up to that collection. For example, a std::vector
invalidates all pointers into it on reallocation, and removal/insertion without reallocation invalidates (well, changes what they point to) all pointers beyond the insertion/removal point. A std::list
on the other hand is stable; pointers only get invalidated if the specific element they point to is removed.
A collection should generally document its invalidation behavior. Unfortunately, the Qt collections don't. Reading their documentation tells us that QMap
is a red-black balanced binary tree, and QHash
is a separate chaining hash table, so they should both have the std::list
behavior for invalidation, but there's no guarantee of that. (For example, QHash
could store the head entry directly in the bucket list, which would mean that rehashing would invalidate most pointers, and removing an element could invalidate pointers to elements in the same bucket.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With