QAbstractListModel memory leak #140
-
|
I ran the modelview_color example under valgrind, and it's leaking QVariant objects. Trimmed output: You can observe memory usage climbing if you keep scrolling the list up and down. It's leaking both the color and string objects. I'm not sure the right way to free them. Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
|
Good catch! My initial thoughts are that we could capture the objects into an array or slice so that they could be freed but even then, I would want to test to see if we could free while the object is in use. Another option would be to pre-create each object (since they're static in this case) and pass those values rather than allocating new instances for each loop. I can add a patch in a few minutes with the latter idea if you want to give it a test? The patch file: mvc.patch When applied to the example code, it looks clean on my end and I can run it through Valgrind later as well if you don't have the time. The patch moves the allocations out of the callback and into the main function. This actually has two benefits: one, we're not allocating at each turn but two, we can control the cleanup of the objects. If possible, this might be worth documenting because in this case, Qt is copying the returned value even though we are allocating it which means there is no owner of the memory, explaining the leak. This is also why we can reuse the same memory for the |
Beta Was this translation helpful? Give feedback.
No need to apologize! And you're right because in typical Qt usage, QVariant is stack-allocated. The extra care we need to take isn't even because of the bindings or the heap allocations but because we've opted in to manual memory management. 😄 This isn't an issue for the bindings for Go because of the garbage collector. Where Qt can have stack-allocated types, all of ours will be heap-allocated and require manual cleanup. The same goes for the QColor objects in the example as they do for the QVariant objects. Based on the results, we could also have a patch with a single global QVariant placeholder and keep most of the original code as it was but replacing the return statements with the …