P
P
Pavel K2018-05-13 00:35:30
Qt
Pavel K, 2018-05-13 00:35:30

Smart pointers, creation in a loop, or what to use instead?

Greetings!
In general, background:
In a separate thread in a cycle, I create a class, generate a signal (Qt emit ....), in the signal parameters, a pointer to this class,
several slots in several different threads are subscribed to this signal.
In order not to free resources later manually (after all the slots have worked), I decided to use smart pointers.
Currently QSharedPointer.
After the transition, an incredibly hard-to-find bug appeared, namely, memory wasting (if you go back, then everything is OK).
And it can work normally for a day, and then start eating up the RAM.
Once upon a time, either in the docks or on the forums, I heard a mention that smart pointers should not be used in cycles.
Purely theoretically, why and is this so?
If so, then how is it better to release resources if it is not known in advance how many slots will be subscribed to the signal and, moreover, which of the slots will subscribe last (what would be released in it)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2018-05-13
@vt4a2h

Once upon a time, either in the docks or on the forums, I heard a mention that smart pointers should not be used in cycles.
Purely theoretically, why and is this so?

I do not see any barriers to use in cycles.
The SharedPointer should release resources itself when the reference count drops to zero. You don't have to control this in the normal case (and if you have to, then use intrusive pointers).
Check for circular references. Run the program with memory analyzers, Valgrind, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question