[patch] Looking for an opinion on std::auto_ptr

Ian Monroe ian at monroe.nu
Mon Oct 20 20:59:45 CEST 2008


On Mon, Oct 20, 2008 at 12:02 PM, Erik Hovland <erik at hovland.org> wrote:
> I am a sucker for std::auto_ptr. Since I know of my weakness, I wanted to check
> the temperature of the rest of the developers. Mainly because auto_ptr
> is not yet
> used.
>
> Anyhow, the attached patch does two things. The first is that the first call to
> delete ec is never executed if ec is valid. That is a minor memory leak since
> it seems as() does dynamically allocate that pointer.
>
> Secondly, it seems auto_ptr could be used for all of the foreach calls in that
> function.
>
> If no one complains in the next 48 hours I will likely commit this
> patch. If there are
> concerns, I am happy to address them.

Like has been already pointed out, we use KSharedPtr. KSharedPtr has a
major advantage over std::auto_ptr in that the reference counter
exists in the object instead of a pointer. So if you have a
KSharedPtr, pass it to a function as a plain pointer and then make
another KSharedPtr of the plain pointer, the reference count remains
accurate (the situation in which this would happen is more complicated
likely).

Also you have to use .get() to see if its null or not? KSharedPtr can
always be used just like a pointer.

That said I don't like that patch regardless since its fixing
something that isn't broken. Creating an object and then deleting it
two lines later seems fine. Personally I think use of reference
counting pointers should be restricted to where they are actually
needed - objects like Meta::Track that get passed around to many
objects and keeping track of it manually would be complicated and
buggy.

Soren, QPointer is just a pointer that never dangles and doesn't do
any memory management.

Ian


More information about the Amarok-devel mailing list