How do I fix a StackOverflowException?

2k Views Asked by At

I am getting a StackOverflowException in my C# program.

Cmodel.cs

public class CModel
{
    public Vector3 Position { get; set; }

    public Vector3 Rotation { get; set; }

    public Vector3 Scale { get; set; }

    public Model Model { get; private set; }

    public BoundingSphere BoundingSphere
    {
        get
        {
            // no need for rotation, as this is a sphere
            Matrix worldTransform = Matrix.CreateScale(Scale) *
            Matrix.CreateTranslation(Position); // THIS IS WHERE THE EXCEPTION OCCURS

            BoundingSphere transformed = BoundingSphere;
            transformed = transformed.Transform(worldTransform);

            return transformed;
        }
    }
    private Matrix[] modelTransforms;
    private GraphicsDevice graphicsDevice;
    private BoundingSphere boundingsphere;
    public CModel(Model Model, Vector3 Position, Vector3 Rotation,
    Vector3 Scale, GraphicsDevice graphicsDevice)
    {
    this.Model = Model;

    modelTransforms = new Matrix[Model.Bones.Count];
    Model.CopyAbsoluteBoneTransformsTo(modelTransforms);

    buildBoundingSphere();
    }


    public void Draw(Matrix View, Matrix Projection)
    {
        // Calculate the base transformation by combining
        // translation, rotation, and scaling
        Matrix baseWorld = Matrix.CreateScale(Scale)
        * Matrix.CreateFromYawPitchRoll(
        Rotation.Y, Rotation.X, Rotation.Z)
        * Matrix.CreateTranslation(Position);
        foreach (ModelMesh mesh in Model.Meshes)
        {
            Matrix localWorld = modelTransforms[mesh.ParentBone.Index]
            * baseWorld;
            foreach (ModelMeshPart meshPart in mesh.MeshParts)
            {
                BasicEffect effect = (BasicEffect)meshPart.Effect;
                effect.World = localWorld;
                effect.View = View;
                effect.Projection = Projection;
                effect.EnableDefaultLighting();
            }
            mesh.Draw();
        }
    }
        private void buildBoundingSphere()
        {
            BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0);

            // Merge all the model's built in bounding spheres
            foreach (ModelMesh mesh in Model.Meshes)
            {
                BoundingSphere transformed = mesh.BoundingSphere.Transform(
                    modelTransforms[mesh.ParentBone.Index]);

                sphere = BoundingSphere.CreateMerged(sphere, transformed);
            }

            this.boundingsphere = sphere;
        }
    }
}
4

There are 4 best solutions below

2
On

You have a recursive call in your getter which will call itself and cause the StackOverflowException :

public BoundingSphere BoundingSphere
{
    get
    {
       ...
       BoundingSphere transformed = BoundingSphere;
       ...
    }
}

It's not completely clear what you meant to write - but if you want to preserve any state you will need a backing field for storing the bounding sphere instead.

0
On
  1. Look at the call stack or the exception's stack trace.
  2. Identify the loop.
  3. Figure out how to break the loop.
0
On

Your get method calls itself: BoundingSphere calls BoundingSphere.

private BoundingSphere _boundingsphere = null;
public BoundingSphere BoundingSphere
{
    get
    {
        // no need for rotation, as this is a sphere
        **Matrix worldTransform = Matrix.CreateScale(Scale)
            * Matrix.CreateTranslation(Position);**

        BoundingSphere transformed = _boundingsphere;
        transformed = transformed.Transform(worldTransform);

        return transformed;
    }
    set
    {
        _boundingsphere = value;
    }
}

When you use following form:

public BoundingSphere BoundingSphere { get; set }

you don't need to specify variable to store actual value, but when you implement get or set explicitly you should declare additional variable and use it in get, set implementations.

0
On

I think you're needing to change

get 
{ 
    ...
    BoundingSphere transformed = BoundingSphere; // public property
    ...
}

to this

get 
{ 
    ...
    BoundingSphere transformed = boundingsphere; // private variable
    ...
}