The bane of Dangling Pointers

Ian Monroe ian.monroe at gmail.com
Mon Dec 21 15:16:58 CET 2009


On Mon, Dec 21, 2009 at 6:53 AM, Ian Monroe <ian.monroe at gmail.com> wrote:
> QSharedPointers can be fairly dangerous if not used correctly. Whereas
> QPointer is to solve the dangling pointer issue, and are pretty much
> "free" for any QObject (they just connect to existing signals),
> QSharedPointers is for memory management. So you have to be mindful
> when using QShared*Pointers/KSharedPtr (basically you must ensure that
> the ref-counting is happening correctly by not passing around the bare
> pointer too much). And I would say its totally overkill if you're only
> worried about dangling pointers.
>
> Also QSharedPointers do their referencing counting inside the pointer
> object. This is inferior to QSharedDataPointer and KSharedPtr which
> hold the reference counter inside the object itself (so you can pass
> the bare pointer, make a QSharedDataPointer or KSharedPtr from it and
> the counting works just fine). So I'd say to always use KSharedPtr,
> unless you are holding a pointer to an object defined in a library
> that you can't derive from QSharedData.

In the above message, do s/QSharedDataPointer/QExplicitlySharedDataPointer/.

Or just use KSharedPtr when you want memory management and don't worry
about it. :)

Ian


More information about the Amarok-devel mailing list