Unexpected QList behaviour with nested structures.
Following strustured types defined:
- typedef struct {
- const char* str;
- } NObj;
- typedef struct {
- NObj v1;
- NObj v2;
- } twoPairs;
then in some class I create private: QList<twoPairs> pairs;
All works fine until I try get values from this list.
I add data using this:
- tmp.v1.str = someptr1;
- tmp.v1.obj = someptr2;
- tmp.v2.str = someptr3;
- tmp.v2.obj = someptr4; // here all is fine, fields are right
- pairs << tmp; // unfortunately QtCreator doesn't show pairs content
but while doing something like this
- foreach( twoPairs pair, pairs )
- {
- char* x1 = pair.v1.str; // is fine
- char* x3 = pair.v2.str; // ----CORRUPTED!!----
- }
And this appears while any other attempt to get structure from QList. Even in:
- tmp = pairs.at(0);
- char* x1 = tmp.v1.str; // is fine
- char* x3 = tmp.v2.str; // ----CORRUPTED!!----
What is wrong with these structures? Isn’t that a bug in QList implementation? Anybody confirm?
10 replies
Your code has a lot of bugs and won’t even compile.
- char* x1 = pair.v1.str; // is fine
No, this is not fine because you assign a const char* to a char* without a const cast.
No, this is even more worse because you assign a QObject pointer to a QObject instance.
From reading your code I’m not surprised that QList shows unexpectet behaviour. But maybe the QList programmer created a serious bug because he was tired after 14 hours of work. ;)
Gourmand, this is the third topic in a short time where you claim very well tested Qt libs have bugs. Perhaps you should mind this item [catb.org] from the Smart Questions FAQ [catb.org].
You cannot have a const pointer and then assign to it normally. Only in the initializer list of constructor or when defining it. Either
or
- struct ABC {
- : obj(obj_param)
- {
- }
- };
- struct ABC a(myObj);
Other issues in the code, you don’t allocate memory anywhere..
Do this:
- struct ABC
- {
- char* str;
- }
- struct ABC a;
- a.str = strdup("your const char* string");
strdup duplicates string, allocates memory and returns it.
If you say, it’s solved, then could you add [ Solved ] in the title.
You can’t read? I already told – real code was different. I was after 14 hours of permanent work and entered wrong code here. All my tens of thousands lines work fine.Anybody can close this thread?
People here are trying to help you Gourmand, so please be nice and watch what you write.
-MariusG
Admin
You must log in to post a reply. Not a member yet? Register here!




