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

Nikos Chantziaras realnc at gmail.com
Wed May 4 20:32:11 CEST 2016


On 04/05/16 21:11, André Somers wrote:
> Op 04/05/2016 om 16:48 schreef Nikos Chantziaras:
>> I've been removing every trance of 'new' and 'delete' from my code and
>> switching to something like:
>>
>>   auto obj = std::make_unique<Type>(ctor args);
>>
>> (Or std::make_shared, depending on the situation.)
>>
>> But for QObjects with a parent, that can go horribly wrong. So I have
>> to do annoying stuff like:
>>
>>   auto dialog = new QDialog(parent);
>>   // work...
>>   delete dialog;
>>
> Assuming the above is one function, you could just do:
>
> auto dialog = QDialog(parent);
> //work
>
> //end of scope, dialog gets deleted

When 'parent' gets deleted before 'dialog', then 'dialog' gets deleted 
even though it's on the stack.

So that doesn't work.






More information about the Interest mailing list