[Interest] more QSharedPointerQuestions
Jason H
scorp1us at yahoo.com
Fri Mar 2 14:07:30 CET 2012
You raise some good points. In truth, I was told to use QSharedPointer, but I had some of the same points you did. His intention (as I understand it) was to use QSharedPointer to stop accidental deletes.
________________________________
From: André Pönitz <andre.poenitz at mathematik.tu-chemnitz.de>
To: Jason H <scorp1us at yahoo.com>
Cc: "interest at qt-project.org" <interest at qt-project.org>
Sent: Thursday, March 1, 2012 5:16 PM
Subject: Re: [Interest] more QSharedPointerQuestions
On Thu, Mar 01, 2012 at 01:34:04PM -0800, Jason H wrote:
> So I have a QObject-derived object tree, like a QObject* tree; I made
> everything QSharedObject<QObject> accordingly.
Why?
QObject ownership is typically handled easily using the parent-child
relationship. Give your objects a parent object and forget about their
individual lifetime. Manage only the parent object's lifetime.
> (I'll just use QObject in this email)
>
> I have a recursive query function. I want to use QSharedPointers so that I
> don't give the pointer directly to the tree because someone might delete it.
Who is "someone", and why may he delete an object that is passed for
inspection?
> (ignoring data(), because at that point in time I can consider it
> intentional)
A 'delete p;' is exactly as intentional as a 'delete p.data();'
A shared pointer does not increase safety in this case.
> However I hit a problem in my recursive search function
> QSharedObject<QObject> QObject::search() when I go to add "this" item
> to the result set, I can only add "this" as a QObject* not
> QSharedPointer<this>.
> How do I work around "this"?
Establish a clear concept of ownership for your objects and make sure
the query function sticks to it, i.e. does not delete things randomly.
I don't think you will still need shared pointers for the task afterwards.
Andre'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120302/7347ecae/attachment.html>
More information about the Interest
mailing list