Krita shared pointers usage

Stefano Bonicatti smjert at gmail.com
Sat Dec 20 19:31:16 UTC 2014


2014-12-20 19:48 GMT+01:00 Dmitry Kazakov <dimula73 at gmail.com>:

> > Hi, Stefano!
>

Hello again to you ;).

>
> 1) Krita is really large, so the interfaces should be unified. E.g. the
> function should be callable by both owner of the object and not-owner. With
> raw pointers it won't work as you can see in example I showed.
>

Well to be fair you can have it since you can access the pointer inside a
shared pointer and pass it, it just would need the rule of "don't delete
raw pointer by hand"
And having less functions with a shared pointer as an argument lets you
understand which ones do do really need to manage that memory.
But weak pointers are fine.

For the other points, well i agree, those are the places where they have to
be used :).


> Probably, this should be fixed in KisWeakSharedPtr. Or we should implement
> a method KisWeakSharedPtr::toSharedPtr(). I'm open for suggestions :)
>

Well as Cyrille was saying it has to be fixed making a check, but since
there's already a copy constructor for weak -> shared, shouldn't we use
that and just put the check before referencing (and if the weak pointer is
invalid setting the shared pointer to NULL)?

Can you fix it in KisWeakSharedPtr? I guess if we copy an invalid weak
> shared pointer, the copy should successfully happen, but the resulting
> pointer should also become invalid.
>
> You can also write a unittest like this:
>
> KisWeakSharedPtr<int> a;
>
> {
>     KisSharedPtr<int> b(new int(1));
>     a = b;
> }
>
> QVERIFY(!a.isValid());
>
> KisWeakSharedPtr<int> c;
> c = a;
> QVERIFY(!c.isValid());
>
> KisSharedPtr<int> d;
> d = a;
> QVERIFY(!d.isValid());
>

Sure, later i'll try, it's quite easy.

Just check the interface of QWeakSharedPointer. You cannot use it directly,
> you always need to convert it into QSharedPointer before any usage. I'm not
> sure we really need this in Krita or, more precisely, we can rewrite this
> amount of code to follow new design. But at least the general idea should
> be used somehow.
>

Got it right then, well... if you acquire an already dead weak pointer and
make it a shared pointer you would have to check if it's null (after the
fixes above are done), so would be basicly the same as checking isValid(),
unless the code is multithreaded, in which acquiring it as a shared pointer
is a must, otherwise after having checked its validity may die just after.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kimageshop/attachments/20141220/fbc54c3a/attachment.html>


More information about the kimageshop mailing list