Linked by Thom Holwerda on Mon 24th Aug 2009 15:06 UTC, submitted by diegocg
Qt 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.
Order by: Score:
Duplicated effort?
by Chicken Blood on Mon 24th Aug 2009 18:41 UTC
Chicken Blood
Member since:
2005-12-21

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.

Reply Score: 3

RE: Duplicated effort?
by vivainio on Mon 24th Aug 2009 18:47 UTC in reply to "Duplicated effort?"
vivainio Member since:
2008-12-26

No, it's auto_ptr / auto_array.

Reply Score: 3

RE: Duplicated effort?
by adkilla on Tue 25th Aug 2009 07:10 UTC in reply to "Duplicated effort?"
adkilla Member since:
2005-07-07

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.

Reply Score: 2

RE[2]: Duplicated effort?
by tramo on Tue 25th Aug 2009 08:03 UTC in reply to "RE: Duplicated effort?"
tramo Member since:
2009-08-25

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?

Reply Score: 1

RE[2]: Duplicated effort?
by FooBarWidget on Thu 27th Aug 2009 16:56 UTC in reply to "RE: Duplicated effort?"
FooBarWidget Member since:
2005-11-11

You definitely can cherry-pick parts. That's exactly what, for example, Phusion Passenger does.

Edited 2009-08-27 16:57 UTC

Reply Score: 2

its 2009
by project_2501 on Mon 24th Aug 2009 21:45 UTC
project_2501
Member since:
2006-03-20

it's 2009 - why are we worrying about memory allocation in a high level language?

Reply Score: 1

RE: its 2009
by ebasconp on Mon 24th Aug 2009 22:08 UTC in reply to "its 2009"
ebasconp Member since:
2006-05-09

Because C++ is useful to write high level languages while giving you full control of everything, including memory management.

Reply Score: 2

RE: its 2009
by ba1l on Tue 25th Aug 2009 13:38 UTC in reply to "its 2009"
ba1l Member since:
2007-09-08

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

Reply Score: 5

RE: its 2009
by elahav on Wed 26th Aug 2009 14:07 UTC in reply to "its 2009"
elahav Member since:
2009-05-28

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

Reply Score: 1

Scoped Pointer
by 3rdalbum on Tue 25th Aug 2009 11:31 UTC
3rdalbum
Member since:
2008-05-26

I thought it was a new rifle-sight attachment for the Wii Remote...

Reply Score: 2

More explanation
by leos on Tue 25th Aug 2009 14:19 UTC
leos
Member since:
2005-09-21

Nice explanation about smart pointer classes in Qt.
http://labs.trolltech.com/blogs/2009/08/25/count-with-me-how-many-s...

Reply Score: 3

WTF is this news?
by fernandotcl on Tue 25th Aug 2009 22:26 UTC
fernandotcl
Member since:
2007-08-12

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.

Reply Score: 1