June 16, 2011

Gourmand Gourmand
Lab Rat
154 posts

Unexpected QList behaviour with nested structures.

 

Following strustured types defined:

  1. typedef struct {
  2.     const char*     str;
  3.     const QObject*  obj;
  4. } NObj;
  5.  
  6. typedef struct {
  7.     NObj v1;
  8.     NObj v2;
  9. } 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:

  1. tmp.v1.str = someptr1;
  2. tmp.v1.obj = someptr2;
  3. tmp.v2.str = someptr3;
  4. tmp.v2.obj = someptr4; // here all is fine, fields are right
  5.  
  6. pairs << tmp; // unfortunately QtCreator doesn't show pairs content

but while doing something like this

  1. foreach( twoPairs pair, pairs )
  2. {
  3.     char* x1 = pair.v1.str; // is fine
  4.     QObject x2 = pair.v1.obj;  // is fine
  5.     char* x3 = pair.v2.str; // ----CORRUPTED!!----
  6.     QObject x4 = pair.v2.obj;  // is fine
  7. }

And this appears while any other attempt to get structure from QList. Even in:

  1.     tmp = pairs.at(0);
  2.     char* x1 = tmp.v1.str; // is fine
  3.     QObject x2 = tmp.v1.obj;  // is fine
  4.     char* x3 = tmp.v2.str; // ----CORRUPTED!!----
  5.     QObject x4 = tmp.v2.obj;  // is fine

What is wrong with these structures? Isn’t that a bug in QList implementation? Anybody confirm?

10 replies

June 16, 2011

koahnig koahnig
Mad Scientist
2099 posts

What makes you believe that this is a QList problem?
It could also be that the memory of the original char * has been released.

June 16, 2011

Gourmand Gourmand
Lab Rat
154 posts

released WHERE? in foreach() or at() ?? and it can’t be released anywhere because it is statically initialized – it contains pointer to static “string”

of course may be I’m just getting tired… working for 14 hours today… may be…

June 20, 2011

Uwe Kindler Uwe Kindler
Lab Rat
64 posts

Your code has a lot of bugs and won’t even compile.

  1. 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.
  1. QObject x2 = tmp.v1.obj;  // is fine

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. ;)

June 21, 2011

Andre Andre
Area 51 Engineer
6031 posts

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].

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 21, 2011

Gourmand Gourmand
Lab Rat
154 posts

Your code has a lot of bugs and won’t even compile.

this was not exactly copy-pasted code, here was just idea, exactly there was my mistake but not in this code, in other place, now all this work fine, sorry

June 21, 2011

jim_kaiser jim_kaiser
Lab Rat
144 posts

  1.  const QObject*  obj;

You cannot have a const pointer and then assign to it normally. Only in the initializer list of constructor or when defining it. Either

  1. const QObject*  obj = <some_qobject_pointer>;

or

  1. struct ABC {
  2.  
  3.     ABC(QObject* obj_param)
  4.     : obj(obj_param)
  5.     {
  6.     }
  7.  
  8.     const QObject*  obj;
  9. };
  10.  
  11. QObject* myObj = new QObject();
  12. struct ABC a(myObj);

Other issues in the code, you don’t allocate memory anywhere..
Do this:

  1. struct ABC
  2. {
  3. char* str;
  4. }
  5.  
  6. struct ABC a;
  7. 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.

June 21, 2011

Gourmand Gourmand
Lab Rat
154 posts

You cannot have a const pointer and then assign to it normally.

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?

June 21, 2011

mariusg mariusg
Ant Farmer
1099 posts

Gourmand wrote:
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

 Signature 

Project Manager - Qt Development Frameworks

June 21, 2011

Gourmand Gourmand
Lab Rat
154 posts

I already don’t need help. When I’ll need – I’ll ask for it. Sorry. Please close this thread.

June 21, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Gourmand: please be patient. All people here are here because they like Qt and in their free time. If you get rude, you will get no answers. So I suggest, you keep being patient.

EDIT: closed the thread

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

 
  ‹‹ Connecting QThreadStorage local data signal to one slot in the same QThread      [SOLVED] ssh connection ››

You must log in to post a reply. Not a member yet? Register here!