Qcache a buggy class?
-
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? -
@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...
-
@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 thenew
operator) is recommended.QCache
willdelete
the objects and free any memory allocations when removing them from the cache. -
(It might also then be good to more-directly mention that heap-allocated objects removed from the cache by calling
take()
have to bedelete
d 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.
-
@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.
-
@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.)
-
@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
-
@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.