Sign in to follow this  
AndyQ

Mesh cloned with Mesh.Clone gets disposed when original mesh gets disposed

Recommended Posts

I'm trying to texture a sphere and have figured out that I need to create a shpere, clone it with texture parameters and then set the necessary. So I'm currently using some of ZMans code (tut 5 I think it was) however when I come to draw the mesh I get an exception - Object reference not set to an instance of an object - on the sphere.DrawSubset call. Examining the sphere object shows that it has been disposed (Dispose = true). So going back to the creation step, I've narrowed it down to the below code:
        public Mesh CreateTexturedSphere(Device device, float radius, int slices, int stacks)
        {
            Mesh nonTexturedMesh = Mesh.Sphere(device, radius, slices, stacks);
            Mesh m = nonTexturedMesh.Clone(nonTexturedMesh.Options.Value, nonTexturedMesh.VertexFormat | VertexFormats.Texture1, nonTexturedMesh.Device);

            SetSphericalTexture(m);
            nonTexturedMesh.Dispose();

            return m;
        }
When I do the nonTexturedMesh.Dispose(); call, my cloned mesh also gets disposed at this point! Whats even wierder, if I change the device used in the Clone to the device passed in, it works (even though that device is what the nonTexturedSphere was created with). Running though ZMans code (built in the same environment), then all works fine. Can any one shed any light on this behaviour? Have I missed out some state flag that somehow links things together? Cheers

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