On Tue, Nov 24, 2015 at 11:26 PM Roy Stogner <royst...@ices.utexas.edu>
wrote:

> We allocate it, so someone has to free it.  We don't know how long an
> app is going to use it, so the library can't free it, so the app has
> to free it.  UniquePtr and AutoPtr make that the default behavior, and
> so prevent accidental memory leaks.
>

Sure - I understand the reason why it's not a raw pointer.  My question was
why it's a UniquePtr instead of an AutoPtr (or better yet a shared_ptr....
which you answered down below).


> > Finally, would it be ok to change it to AutoPtr
>
> We deprecated AutoPtr; when C++11 is enabled it's a UniquePtr under
> the hood now.
>
> This is nice in some ways (you can just toss them in containers if you
> know you're using C++11) and debatable in others (you can't hand off
> ownership trivially or accidentally).
>

Actually - this is the problem.  This isn't really true.  If you put a
UniquePtr in a container... and then you get it out of the container by
copy (like you would for a normal pointer)... it _removes_ it from the
container (leaves a NULL pointer behind in the UniquePtr in the
container).  That _sucks_.

I hit that just yesterday and got a nice segfault from it (first time
through the loop everything worked swimmingly... second time through got a
segfault because it picked up a NULL pointer out of the vector these things
were stored in).


> > (or a shared_ptr, do we use those in libMesh yet?).
>
> We test for them and define LIBMESH_HAVE_CXX11_SHARED_PTR but since
> they're not in  C++03 we don't use them in library code.  I love
> shared_ptr but I'm too lazy to strip out the Boost version to give us
> a fallback.
>

The time for deprecating _C++03_ support is coming soon.  Shared pointers
are awesome.  Even I, who normally hates this kind of stuff, now ONLY use
std::shared_ptr in all of my own code.  No more raw pointers anywhere.
 (Not that raw pointers are evil, but that shared_ptrs are _awesome_).

Anyway - thanks for the discussion guys... I think I get it.  I'll continue
to just call release() on every Auto/UniquePtr I get from libMesh (like I
always have).  The only difference is I'll now wrap that release() in a
nice std::shared_ptr ;-)

Derek
------------------------------------------------------------------------------
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
_______________________________________________
Libmesh-devel mailing list
Libmesh-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libmesh-devel

Reply via email to