May 1, 2011

situ117 situ117
Lab Rat
42 posts

Smart pointers in Qt

Page  
1

Hello,

I was reading about QSharedPointer in Qt. But I’ve not seen it much in use in source code of Examples and Demos. If we have smart pointers in Qt, why old C type pointers are preferred ?

17 replies

May 1, 2011

secretNinja secretNinja
Lab Rat
141 posts

This is quite informative on the subject. [stackoverflow.com]

May 2, 2011

leon.anavi leon.anavi
Mad Scientist
1200 posts
situ117 wrote:
I was reading about QSharedPointer in Qt. But I’ve not seen it much in use in source code of Examples and Demos.

There are not so much Qt examples and demos with QSharedPointer because of the general concept for memory management in Qt using parent–child hierarchy of QObject.

A good article about available smart pointers in Qt and their purposes is: Count with me: how many smart pointer classes does Qt have? [labs.qt.nokia.com]

Cheers,
Leon

 Signature 

http://anavi.org/

May 2, 2011

koahnig koahnig
Area 51 Engineer
2881 posts

Thanks for starting the topic. Especially the link presented by Leon is very informative.
Quite clear is the rationale of Qt for using their own smart pointers. There is no way to rely on external definitions as boost as long as these are not standard part of almost all compilers.
However, I am wondering, if there are strong arguments for switching to Qt’s smart pointers when already using boost shared_ptr?
Any opinion of someone knowing the details of both implementations?

May 2, 2011

Franzk Franzk
Lab Rat
830 posts

Both the QSharedPointer and the boost shared_ptr implement the same principle. There is no good reason to switch to one or the other unless you plan on ditching either of the libraries in the long run.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

May 2, 2011

leon.anavi leon.anavi
Mad Scientist
1200 posts
koahnig wrote:
Thanks for starting the topic. Especially the link presented by Leon is very informative.

I am glad that I was able to help :)

Franzk wrote:
Both the QSharedPointer and the boost shared_ptr implement the same principle. There is no good reason to switch to one or the other unless you plan on ditching either of the libraries in the long run.

I also share the opinion that sharing frameworks is not good idea. Qt is excellent framework (of course so is Boost) and Qt has all needed tools to create whatever you need.

Best regards,
Leon

 Signature 

http://anavi.org/

May 2, 2011

koahnig koahnig
Area 51 Engineer
2881 posts

@Franzk and @Leon, thanks for sharing your opinion on this. Best regards koahnig

June 29, 2011

Duck Duck
Lab Rat
107 posts

The question is rather “why would you want to use smart pointers if plain pointers are completely sufficient”. Smart pointers do not come for free, both time- and space-wise. Some operations are as quick/as small as for plain pointers, some are significantly more expensive. No need to pay that cost if there is no gain.

June 30, 2011

Gerolf Gerolf
Robot Herder
3235 posts
Duck wrote:
The question is rather “why would you want to use smart pointers if plain pointers are completely sufficient”. Smart pointers do not come for free, both time- and space-wise. Some operations are as quick/as small as for plain pointers, some are significantly more expensive. No need to pay that cost if there is no gain.

It depends what you do if plain pointer are sufficient. YOu can surely always use plain pointers and do everything on your own, but why should you do that?

E.g. shared pointers: The objects are deleted, if the last shared pointer goes out of scope. If you do that with plain pointers, you have to implement ref counting etc. And there are many more types of shared pointers, e.g. QPOinter which goes to 0 if the object is destroyed from somewhere etc.

From my opinion, smart pointer are nearly always good to use!

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

June 30, 2011

Andre Andre
Robot Herder
6296 posts

Then again, C++ 0x allows compilers to generate garbage collection. If you have that, do you still need smart pointers for that?

Obviously, the problem is that it is optional: “implementation dependent”. That means that a library like Qt can’t rely on it.

Smart pointers are not always a good idea to use, but IMHO, they often are.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 30, 2011

koahnig koahnig
Area 51 Engineer
2881 posts
Duck wrote:
The question is rather “why would you want to use smart pointers if plain pointers are completely sufficient”. Smart pointers do not come for free, both time- and space-wise. Some operations are as quick/as small as for plain pointers, some are significantly more expensive. No need to pay that cost if there is no gain.

The expence of smart pointers is certainly a reason to think twice.

Andre wrote:

Smart pointers are not always a good idea to use, but IMHO, they often are.

Do you have an example?
Certainly it is not always the first choice to use pointers respectively smart pointers as already discussed above. I am wondering, if there are further situations where they are really not a good choise?

June 30, 2011

Andre Andre
Robot Herder
6296 posts
koahnig wrote:
Andre wrote:
Smart pointers are not always a good idea to use, but IMHO, they often are.
Do you have an example? Certainly it is not always the first choice to use pointers respectively smart pointers as already discussed above. I am wondering, if there are further situations where they are really not a good choise?

One example may be the case where you store lots of pointers to objects in a container class. But, it all depends on your use case.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 30, 2011

Gerolf Gerolf
Robot Herder
3235 posts

If you are in a time critical, real time issue, the indirection you get and perhaps the additional things to do, may hurt, as they cost time.

But in mobile and desktop apps, I think, it does not hurt.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

June 30, 2011

koahnig koahnig
Area 51 Engineer
2881 posts
Andre wrote:
One example may be the case where you store lots of pointers to objects in a container class. But, it all depends on your use case.

That is a good example to be careful with smart pointers. If somehow the object/container survives so does the smart pointer and the allocated memory.
So a conclusion would be:
watch out for run-away containers ;-)

Gerolf wrote:
But in mobile and desktop apps, I think, it does not hurt.

Well, if you are not careful enough, the run-away container may get you ultimately. Several GBs of memory are en vogue today, but ultimately you can drain any big pond (as long as you do not call it ocean) ;-)

June 30, 2011

Andre Andre
Robot Herder
6296 posts

koahnig wrote:

Well, if you are not careful enough, the run-away container may get you ultimately. Several GBs of memory are en vogue today, but ultimately you can drain any big pond (as long as you do not call it ocean) ;-)


Off topic:
See here [google.nl] for how such a thing looks…

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 30, 2011

Gerolf Gerolf
Robot Herder
3235 posts
koahnig wrote:
Gerolf wrote:
But in mobile and desktop apps, I think, it does not hurt.

Well, if you are not careful enough, the run-away container may get you ultimately. Several GBs of memory are en vogue today, but ultimately you can drain any big pond (as long as you do not call it ocean) ;-)

But itÄs the same with pure pointers. You have to watch out to delete allocated memory. No chance to get around this problem, this or that way.

Where I love smart pointers is if you have some function that needs to allocate memory to read some values from Windows API where yxou have to pre allocate the memory. Just use some auto pointers and it will be destructed correctly when you leave the function, even in exception handling cases.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

Page  
1

  ‹‹ Redistribute a dynamic library: c++ in header and issues when mixing compilers      My sub-sub-class is not being included ››

You must log in to post a reply. Not a member yet? Register here!