Krita shared pointers usage
Cyrille Berger
cberger at cberger.net
Sat Dec 20 16:55:44 UTC 2014
On Saturday 20 December 2014 13:58:57 Stefano Bonicatti wrote:
> In general i think the same as him ->
> http://www.stroustrup.com/C++11FAQ.html#std-shared_ptr, not only because he
> his who he his, but because it actually make sense what he says about
> making the object lifetime cycle complex when shared pointer are used so
> often.
The problem of Stroustrup is that he probably never was involved in a very
large project involving many people who come and go during the years. So what
he says, he is very sound and good at a theoretical level, and I would agree
that in an ideal world I would follow what he says. And I do for small
project. But Krita is 300k lines (or so) and had about 30 different developers
working on it at different point of time. And not only the architecture has
never been documented, it changes all the time, in such a situation, real life
kicks in, and shared pointers are just better.
> Maybe having unique_ptr like pointers and shared pointer passed as const
> references instead of raw pointer would be better,
Definitely better. But it make the code a bit less readable, unless you
introduce a typedef. But is it worth the amount of work? (I guess it could be
a new guideline).
> so that it's more
> explicit the fact that you should not delete it manually and it's cheaper,
> copy wise and with the ref/deref not happening when passed around in code
> that shouldn't actually prolong its life.
> You would still have shared pointers around but it would be much easier for
> the memory leak tracker to keep track on who is really holding the memory.
When it comes to memory leak and optimisation, I always advise to first use a
profiler to get information and then find solution. If it shows that their are
actual (major) leak and that the referencing/derefencing is a problem. Because
you may spend month trying to fix that doesn't change anything noticeable while
in the same time you could have fixed actual problems.
> > The idea provided by Qt's shared pointers might be very helpful here
>
> (Qt's weak shared pointer doesn't allow dereferencing the pointer until
> upgrade to a real shared pointer)
>
> There are a couple of problems with Krita weak pointers i'm facing though:
> 1) If you pass a dead weak pointer to a shared pointer the check to see if
> the weak pointer is valid is done in an assert only after the shared
> pointers tries to increase the reference count.
> This works if the weak pointer internal pointer was set to NULL, because
> shared pointer ref() function checks for that, but if the weak pointer
> points to deleted memory.. well ref() behaviour is undefined.
This is a bug, obviously. It should assert if the weak shared pointer is not
valid.
--
Cyrille Berger Skott
More information about the kimageshop
mailing list