Sign in to follow this  
Raeldor

Smart Pointers the Way to Go?

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this