Smart Pointers the Way to Go?

Started by
3 comments, last by mfawcett 18 years, 7 months ago
Hi All, Ok, so I have a mesh class that has pointers to arrays of vertices, uv co-ordinates etc. I want to be able to share an array of UV's (for instance) across meshes. The problem is that I don't want to free vertices that may be used by another mesh during the destructor class. Would smart pointers help me here? I understand they can keep track of the number of references and only delete once it has reached zero. Is this automatic like using a managed language such as c#? Any advice here would be great. Thanks [Edited by - Raeldor on September 5, 2005 7:48:32 PM]
Advertisement
I use them in my 3D engine and it has really helped with memory leaks. However, it's not as comprehensive as garbage collection schemes because the typical smart pointer class can't deal with circular dependencies. I use boost shared_ptr, which has a weak pointer class that tries to deal with this problem.

Just be careful how you use them with functions
Steven ToveySPUify | Twitter
hm if i get you right, you want something like:
struct vertex
{
smart_ptr<float> u, v;
smart_ptr<float> x, y, z;
..
};

or something similar.
if this is the case you might have to reconsider, because when you render the mesh, you
need to have it all in a vertex "steam", and now you only got a stream with pointers.
so when youre going to render a mesh with vertices like the one above, you would have to do something like:
struct vertex_data { float u, v, x, y, z; }
vertex_data seriallize(vertx *v)
{
vertex_data vd;
vd.u = *v->u;
vd.v = *v->v;
..
}

which would be quite slow
----------Thale Cres
Quote:Original post by chot
hm if i get you right, you want something like:

...snip...

I think you got him wrong. I'm pretty sure the OP wants a shared pointer to the vertex arrays, not a pointer per vertex. He is making a mesh class afterall.

(BTW you may want shared_array instead, or shared_ptr with a custom deleter)
--Michael Fawcett

This topic is closed to new replies.

Advertisement