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

Nikos Chantziaras realnc at gmail.com
Thu May 5 12:32:27 CEST 2016


I completely disagree with you :-)

I hope you're not the type of guy who writes code like this:

   something;
   if (!something) {
       delete;
       return;
   }
   something else;
   if (!something else) {
       delete;
       return;
   }
   try {
      do stuff;
   }
   catch (...) {
       delete;
       throw;
   }

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.


On 05/05/16 10:18, Bo Thorsen wrote:
> I'm still not sure if I should respond to this or not. You're clearly
> trying to solve an unsolvable problem.
>
> This mail is going to sound like a rant, because I've heard those types
> of arguments from Qt newbies again and again. But it's actually not.
> There's information here that will lead you to a code style that fits
> the Qt memory model. Not your memory model.
>
> First, you need to listen to this and fully understand it: There is *no*
> way you can code C++ so memory errors are impossible.
>
> Using smart pointers is just a silly pipe dream of the nineties that
> unfortunately hasn't gone away. As Herb Sutter (I think, not completely
> sure) once said: "Smart pointers is a solution in search of a problem".
>
> Smart pointers is good for one thing only: To delete objects that
> control their own life span.
>
> For all other - and that's 99.9% - objects, it's cleaner simpler and
> more efficient if you get rid of the notion that a system can do it for
> you, and start giving clear lifetime ownership of your objects.
>
> Yes, you can get crashes or memory loss. That's why you don't hire crap
> coders to do C++, and check your code with tools like valgrind.
>
> I'm sure you will have a long list of objections as to why I'm
> completely wrong. Shared pointer lovers usually do. I don't care. I've
> been doing Qt coding for 20 years and have tried every trick available
> and come to the conclusion that it's actually only up to me writing good
> code.
>
> I hope this helps. I give the same type of arguments to my customers.
> The smart ones get it, the others pay with increased maintenance cost
> over the years.
>
> Bo Thorsen,
> Director, Viking Software.
>





More information about the Interest mailing list