Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Qcache a buggy class?
QtWS25 Last Chance

Qcache a buggy class?

Scheduled Pinned Locked Moved Unsolved Qt 6
9 Posts 5 Posters 792 Views
  • 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.
  • C Offline
    C Offline
    Chitra
    wrote on 26 Apr 2024, 05:18 last edited by
    #1

    I am using Qcache class to fill QString data.
    Have set cache maxcost as 30,whenever I try to insert 31st element it crashes with some 'invalid free() ' error,and whenever I try to access my cache variable from other class with.object(key) api it crashes with error "malloc(): unaligned tcache chunk detected" though key is present in cache ,that have verified with .contains(key) api .Pls suggest if this is buggy class?

    J 2 Replies Last reply 26 Apr 2024, 05:20
    0
    • C Chitra
      26 Apr 2024, 05:18

      I am using Qcache class to fill QString data.
      Have set cache maxcost as 30,whenever I try to insert 31st element it crashes with some 'invalid free() ' error,and whenever I try to access my cache variable from other class with.object(key) api it crashes with error "malloc(): unaligned tcache chunk detected" though key is present in cache ,that have verified with .contains(key) api .Pls suggest if this is buggy class?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Apr 2024, 05:20 last edited by
      #2

      @Chitra Please show your code and exact error messages you get.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Chitra
        26 Apr 2024, 05:18

        I am using Qcache class to fill QString data.
        Have set cache maxcost as 30,whenever I try to insert 31st element it crashes with some 'invalid free() ' error,and whenever I try to access my cache variable from other class with.object(key) api it crashes with error "malloc(): unaligned tcache chunk detected" though key is present in cache ,that have verified with .contains(key) api .Pls suggest if this is buggy class?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 26 Apr 2024, 06:56 last edited by
        #3

        @Chitra And also take a look at https://forum.qt.io/topic/156326/qcache-is-not-allowing-to-add-item-when-maxcost-is-reached-and-qcache-api-object-crashes/2 - I guess you have exact same issue...

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        F 1 Reply Last reply 27 Apr 2024, 23:43
        0
        • J jsulm
          26 Apr 2024, 06:56

          @Chitra And also take a look at https://forum.qt.io/topic/156326/qcache-is-not-allowing-to-add-item-when-maxcost-is-reached-and-qcache-api-object-crashes/2 - I guess you have exact same issue...

          F Offline
          F Offline
          FeRDNYC
          wrote on 27 Apr 2024, 23:43 last edited by
          #4

          @jsulm That seems a likely assessment.

          The documentation includes an example of how to add data to a QCache...

          For example, here's the definition of a cache that stores objects of type Employee associated with an integer key:

          QCache<int, Employee> cache;
          

          Here's how to insert an object in the cache:

          Employee *employee = new Employee;
          employee->setId(37);
          employee->setName("Richard Schmit");
          ...
          cache.insert(employee->id(), employee);
          

          ...But I wonder if it should perhaps do more to explain why adding heap-allocated objects is important, or why adding stack-allocated objects will lead to program errors?

          Perhaps a statement something like...?

          TIP (or even WARNING?): QCache takes ownership of existing objects inserted into the cache, without constructing new objects or copying data. The lifetime of objects on the cache is expected to be as long as the cache itself. If objects on the cache are destroyed prematurely, later attempts to access or remove those objects will result in program errors.
          Inserting heap-allocated objects (created with the new operator) is recommended. QCache will delete the objects and free any memory allocations when removing them from the cache.

          F 1 Reply Last reply 27 Apr 2024, 23:53
          0
          • F FeRDNYC
            27 Apr 2024, 23:43

            @jsulm That seems a likely assessment.

            The documentation includes an example of how to add data to a QCache...

            For example, here's the definition of a cache that stores objects of type Employee associated with an integer key:

            QCache<int, Employee> cache;
            

            Here's how to insert an object in the cache:

            Employee *employee = new Employee;
            employee->setId(37);
            employee->setName("Richard Schmit");
            ...
            cache.insert(employee->id(), employee);
            

            ...But I wonder if it should perhaps do more to explain why adding heap-allocated objects is important, or why adding stack-allocated objects will lead to program errors?

            Perhaps a statement something like...?

            TIP (or even WARNING?): QCache takes ownership of existing objects inserted into the cache, without constructing new objects or copying data. The lifetime of objects on the cache is expected to be as long as the cache itself. If objects on the cache are destroyed prematurely, later attempts to access or remove those objects will result in program errors.
            Inserting heap-allocated objects (created with the new operator) is recommended. QCache will delete the objects and free any memory allocations when removing them from the cache.

            F Offline
            F Offline
            FeRDNYC
            wrote on 27 Apr 2024, 23:53 last edited by
            #5

            (It might also then be good to more-directly mention that heap-allocated objects removed from the cache by calling take() have to be deleted by the caller.)

            Yeah, the QCache::take() docs say this:

            The ownership of the returned object is passed to the caller.

            But that could be more explicit.

            ...Or I suppose the docs could have a general explainer page about object ownership and lifetimes, which statements like the one quoted above are linked to. Devs moving from garbage-collected languages, in particular, don't necessarily have that background.

            C K 2 Replies Last reply 28 Apr 2024, 06:29
            0
            • F FeRDNYC
              27 Apr 2024, 23:53

              (It might also then be good to more-directly mention that heap-allocated objects removed from the cache by calling take() have to be deleted by the caller.)

              Yeah, the QCache::take() docs say this:

              The ownership of the returned object is passed to the caller.

              But that could be more explicit.

              ...Or I suppose the docs could have a general explainer page about object ownership and lifetimes, which statements like the one quoted above are linked to. Devs moving from garbage-collected languages, in particular, don't necessarily have that background.

              C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 28 Apr 2024, 06:29 last edited by
              #6

              @FeRDNYC said in Qcache a buggy class?:

              But that could be more explicit.

              That's the normal phrase in c++ that you're now repsonsible for the object.

              ...But I wonder if it should perhaps do more to explain why adding heap-allocated objects is important, or why adding stack-allocated objects will lead to program errors?

              And also this is basic c++ stuff, nothing Qt specific.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              F 1 Reply Last reply 28 Apr 2024, 13:48
              2
              • C Christian Ehrlicher
                28 Apr 2024, 06:29

                @FeRDNYC said in Qcache a buggy class?:

                But that could be more explicit.

                That's the normal phrase in c++ that you're now repsonsible for the object.

                ...But I wonder if it should perhaps do more to explain why adding heap-allocated objects is important, or why adding stack-allocated objects will lead to program errors?

                And also this is basic c++ stuff, nothing Qt specific.

                F Offline
                F Offline
                FeRDNYC
                wrote on 28 Apr 2024, 13:48 last edited by
                #7

                @Christian-Ehrlicher said in Qcache a buggy class?:

                That's the normal phrase in c++ that you're now repsonsible for the object.

                Oh, I'm well aware, but if @jsulm is correct that this question and that other one are effectively duplicates, then that's two questions in just a single day from devs who got tripped up by lifetime issues — despite "the normal phrase" being part of the docs.

                That makes me think the docs could be doing better, since the normal phrase doesn't seem to be entirely sufficient on its own.

                A more-detailed explanation doesn't have to live in the Qt docs, of course. It could perhaps be a link to the cppreference page(s) on lifetime. (Although documentation that isn't strict API reference can be a bit dense and impenetrable, there.)

                C 1 Reply Last reply 28 Apr 2024, 14:09
                0
                • F FeRDNYC
                  28 Apr 2024, 13:48

                  @Christian-Ehrlicher said in Qcache a buggy class?:

                  That's the normal phrase in c++ that you're now repsonsible for the object.

                  Oh, I'm well aware, but if @jsulm is correct that this question and that other one are effectively duplicates, then that's two questions in just a single day from devs who got tripped up by lifetime issues — despite "the normal phrase" being part of the docs.

                  That makes me think the docs could be doing better, since the normal phrase doesn't seem to be entirely sufficient on its own.

                  A more-detailed explanation doesn't have to live in the Qt docs, of course. It could perhaps be a link to the cppreference page(s) on lifetime. (Although documentation that isn't strict API reference can be a bit dense and impenetrable, there.)

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 28 Apr 2024, 14:09 last edited by Christian Ehrlicher
                  #8

                  @FeRDNYC said in Qcache a buggy class?:

                  in just a single day from devs who got tripped up by lifetime issues

                  From my pov this is just one person or a collegues - I've seen this more than once here.
                  We're not here to explain basic c++ concepts (where ownerhsip is one of the most basic ones) but Qt - without proper basic c++ knowledge you won't have fun with Qt at all. So I don't see why the Qt docs should discuss such basic principles of the underlying programming language. It's not a Qt problem at all:

                  std::vector<Foo*> fooContainer;
                  {
                    Foo foo;
                    fooContainer.push_back(&foo);
                  }
                  fooContainer[0]->doSomething(); // crash
                  

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  4
                  • F FeRDNYC
                    27 Apr 2024, 23:53

                    (It might also then be good to more-directly mention that heap-allocated objects removed from the cache by calling take() have to be deleted by the caller.)

                    Yeah, the QCache::take() docs say this:

                    The ownership of the returned object is passed to the caller.

                    But that could be more explicit.

                    ...Or I suppose the docs could have a general explainer page about object ownership and lifetimes, which statements like the one quoted above are linked to. Devs moving from garbage-collected languages, in particular, don't necessarily have that background.

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 30 Apr 2024, 09:34 last edited by
                    #9

                    @FeRDNYC said in Qcache a buggy class?:

                    ...Or I suppose the docs could have a general explainer page about object ownership and lifetimes, which statements like the one quoted above are linked to. Devs moving from garbage-collected languages, in particular, don't necessarily have that background.

                    Qt is a library, and as @Christian-Ehrlicher mentioned, it's not any library's concern to teach you the underlying language - it is assumed that you're going to be using the library after you understand the basis of its existence.

                    I can see how if you are coming from a garbage-collected language this may sound unfair, but if you think about it for a while I think you'd agree that C++ is too large and too complex for you to require from Qt (developers) to feed you information about the language too.

                    Note: I'm liberally using "you", but do consider it as using the indefinite pronoun form.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    2

                    1/9

                    26 Apr 2024, 05:18

                    • Login

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