[Development] Setters: Clarifying the ownership

Jesus Fernandez jesus.fernandez at qt.io
Fri Jan 19 16:15:52 CET 2018


Hi all!

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.


--
Best regards,
Jesús
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20180119/b89c7002/attachment.html>


More information about the Development mailing list