[Development] Setters: Clarifying the ownership
jesus.fernandez at qt.io
Fri Jan 19 16:15:52 CET 2018
I always found something annoying in the Qt API. The problem comes with the setters of our properties. When I want to pass an object to a property I never know if I need to take care of the object or relay on parenting system to avoid memory leaks.
To know if the object is going to be reparented, I open the assistant and look for the setter to try to find the famous "takes ownership of" in the function description.
Mårten Nordheim and I were talking about possible solutions to this problem. Typical things came to the discussion:
- adding a macro like Q_TAKES_OWNERSHIP to the function that expands to nothing
- wrapping the parameters with a template class (gsl::owner<T>)
After some discussion he came with the idea of add a different "verb" to the setter, replace "set" with "give". So when we are giving the ownership of an object to instead of setSomething(&object); we will write giveSomething(&object); I really like this solution, it will improve a lot the readability of the client (and internal) code.
For example: QCoreApplication::setEventDispatcher will be QCoreApplication::giveEventDispatcher.
Of course at the beginning this will be a new function and the old set* functions will be kept, but marked as deprecated.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Development