[Interest] What's the recommended way to put QObjects in smart pointers?

Nye kshegunov at gmail.com
Thu May 5 13:20:58 CEST 2016


On Thu, May 5, 2016 at 4:22 AM, Nikos Chantziaras <realnc at gmail.com> wrote:
>
> On 05/05/16 01:44, Nye wrote:
>
>> No one said it does. Perhaps I'm misunderstanding, are you searching for
>> a silver bullet for memory management?
>>
>
> I already found it. Question is how to make Qt work with it :-)
>
>
You most certainly have not. I'm tired of repeating this, but here I go
again: C++ IS NOT JAVA!


>
>  > That's not good enough. I need to delete objects way before their
>> parent is deleted.
>>
>> So delete them. As I said, parent is notified of the child's deletion.
>>
>
> If I delete them, the parent may already have deleted them. So I'd have a
> double-delete.


We are running in a circle here. If the parent may delete the children
before you've lost all the references to them, then use QPointer.


>  > When 'parent' gets deleted before 'dialog', then 'dialog' gets
>> deleted even though it's on the stack.
>>
>> And how does the parent gets deleted before the 'dialog' if I may ask,
>> unless you're doing deletions from different threads ...?
>>
>
> I don't know. There's long call chains. Someone may delete the parent.
> There's no way to know.
>

The whole premise behind Mr. Sommers' code was that this is in one function
(one stack frame), so unless you do `delete this` in the middle of the
function (directly or indirectly) there doesn't seem be a way the parent
gets deleted.


> Anyway, the question was whether Qt had something already in place. I now
> see I have to roll my own solution.


It has, it provides a battery of wrapper classes for pointers usable for
different cases. However I get the feeling this won't be just enough for
you.

Well, basically what I want is not having even a single 'new' or 'delete'
> anywhere in my code. I already achieved that with all my non-Qt projects.
>
By now, I consider application-level code that uses 'new' or 'delete' as
> bogus and in need of fixing.


Refer to my first comment at the very top of this mail, I know it's getting
repetitive, but ... C++ is not Java!

I completely disagree with you :-)
>
> I hope you're not the type of guy who writes code like this:
> [snippet]
> This is a nightmare, and this is C++ code that's indeed probably from 20
> years ago. If somebody writes code that like that today, I would want to
> hire them.


I would refer you the Qt's source, I'm sure you'll be terrified of the
amount of raw pointers used. Forgive my bluntness, but what would your
suggestion be then? Scoped pointers can be used, but that's not much
different that manually deleting the objects. And shared pointers are for
things you share, not for things you're too lazy to delete ...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160505/4dd4049a/attachment.html>


More information about the Interest mailing list