Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unexpected QList behaviour with nested structures.
Qt 6.11 is out! See what's new in the release blog

Unexpected QList behaviour with nested structures.

Scheduled Pinned Locked Moved General and Desktop
11 Posts 7 Posters 6.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gourmand
    wrote on last edited by
    #1

    Following strustured types defined:

    @typedef struct {
    const char* str;
    const QObject* obj;
    } 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
    QObject x2 = pair.v1.obj; // is fine
    char* x3 = pair.v2.str; // ----CORRUPTED!!----
    QObject x4 = pair.v2.obj; // is fine
    }@

    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
    QObject x2 = tmp.v1.obj; // is fine
    char* x3 = tmp.v2.str; // ----CORRUPTED!!----
    QObject x4 = tmp.v2.obj; // is fine
    @

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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gourmand
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • U Offline
          U Offline
          Uwe Kindler
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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":http://www.catb.org/~esr/faqs/smart-questions.html#id478549 from the "Smart Questions FAQ":http://www.catb.org/~esr/faqs/smart-questions.html.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Gourmand
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jim_kaiser
                wrote on last edited by
                #7

                @ 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

                @
                const QObject* obj = <some_qobject_pointer>;
                @

                or

                @
                struct ABC {

                ABC(QObject* obj_param)
                : obj(obj_param)
                {
                }
                
                const QObject*  obj;
                

                };

                QObject* myObj = new QObject();
                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Gourmand
                  wrote on last edited by
                  #8

                  bq. 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?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mgran
                    wrote on last edited by
                    #9

                    [quote author="Gourmand" date="1308662520"]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?[/quote]

                    People here are trying to help you Gourmand, so please be nice and watch what you write.

                    -MariusG
                    Admin

                    Project Manager - Qt Development Frameworks

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      Gourmand
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        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

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved