Qt usually takes the boring memory allocation and deallocation from you, either through its implicitly shared containers, or with QObject’s parent child relationship model. But every once in a while, we need to allocate something on the heap, and then the stress starts – where do we delete it, and how do we make sure to not leak the memory? To fix this problem, QScopedPointer was born. It will delete the object it is pointing to automatically when it goes out of scope.
It looks to me like std::shared_ptr in the boost library does the same job. This is due to appear as a part of the standard in C++ 00x.
OTOH, I’m not sure there is a simple way to make std::shared_ptr give up ownership of an object.
No, it’s auto_ptr / auto_array.
This is mainly to provide better support for Qt on platforms like Symbian that would not have TR1/C++0x class library support.
Boost has an ugly API and you can’t dissect and pick one portion of the library separately to use. You have to include the whole Boost library as it is interdependent within itself.
It’s *extremely* easy to cherry pick different parts of Boost and include them in your project. That’s what ‘bcp’ is for, and it works like a charm. Yes, it will pull in a lot of headers (partly because boost shares a lot of things, and otherwise because ‘bcp’ is conservative about what it includes), but the actual *code* it pulls in is minimal. Try doing this with another general purpose library (not even talking about the horrible Qt, which *does* pull in a lot of crap; now *that’s* a heavy dependency).
Boost has a horrible implementation (the price you pay for performance), but boost APIs are always very minimal. (what API is there to shared pointers anyway?) And who cares about ugly implementations if they are well tested and work?
You definitely can cherry-pick parts. That’s exactly what, for example, Phusion Passenger does.
Edited 2009-08-27 16:57 UTC
it’s 2009 – why are we worrying about memory allocation in a high level language?
Because C++ is useful to write high level languages while giving you full control of everything, including memory management.
Because, at some point, someone has to care about memory management.
Qt generally handles memory management. A programmer writing a Qt application generally doesn’t have to worry too much about it. Most objects do not need to be created with the new operator, so they are automatically destroyed when they go out of scope (end of function, or their containing object is destroyed). Most objects allocated on the heap have a clear owner – if you set the owner correctly, Qt will automatically destroy the object when it’s owner is destroyed. Almost all of your memory management will be handled for you, either but C++ itself, or by Qt. There are some cases where it won’t.
The article describes a smart pointer implementation that’s been added to Qt. There are pretty common in modern C++ – essentially, they enforce ownership policies, and destroy the referenced object when appropriate. There are many types of smart pointers, each of which has slightly different behaviour. Using these, a programmer doesn’t have to bother with the actual nuts and bolts of memory management – you just tell the compiler how to manage it, and let it handle everything for you.
Memory management can still cause problems in garbage collected languages like Java or .NET. That’s why .NET has separate finalizers and the IDisposable interface, why C# has the using keyword, why they both have weak references, and so on. There are a multitude of ways to create memory leaks or resource leaks in a garbage collected languages, and it can often be very difficult to work out where the leaks are coming from.
Sure, you can write Java / .NET code while being completely ignorant of memory management. The garbage collector will save you from some problems (objects being destroyed while still referenced, and dealing with shared ownership), but it will not (and can not) prevent memory leaks, or excessive memory usage. You need to be just as careful writing a large Java / .NET application as you do writing a large C++ application.
Edited 2009-08-25 13:39 UTC
Who says C++ is a high-level language? C++ is used in embedded systems where latency is important and memory is tight.
One of the nice (or scary, depending on your point of view) things about C++ is that it can be as high- or low-level as you wish it to. Depending on the features of the language that you use in your code, C++ can be used to write very low-level code adhering to both speed and size constraints.
Edited 2009-08-26 14:08 UTC
I thought it was a new rifle-sight attachment for the Wii Remote…
Nice explanation about smart pointer classes in Qt.
http://labs.trolltech.com/blogs/2009/08/25/count-with-me-how-many-s…
WTF is this newsworthy? A scoped pointer is one of the most basic elements of RIIA-based C++ programming, any decent programmer knows how to implement it or use std::auto_ptr, boost::scoped_ptr or any of the 400 gazillion variations available EVERYWHERE.
No, I can’t stress this enough, this is the most retarded piece of news I’ve seen here in a while.