2012-06-03 41 views
-2

我在C#程序中得到StackOverflowException如何修復StackOverflowException?

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; 
     } 
    } 
} 
+0

的第一件事,任何一個程序員應該做的是檢查堆棧。從OP的問題看來,這似乎還沒有完成(堆棧跟蹤沒有發佈)。我的答案提供者找到這類問題的來源。如果OP不願意嘗試幫助自己(如投票下降所示),我們應該嘗試幫助他。 –

回答

5

你在你的getter遞歸調用,它將調用本身並導致StackOverflowException

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

這不是完全清楚你意味着寫 - 但如果你想保存任何狀態,你需要一個支持字段來存儲邊界球體。

+0

當我這樣做時,它給了我一個實際的錯誤,說''CModel.BoundingSphere.get':並非所有的代碼路徑都返回一個值' – Nate

+0

@Nate:當你做什麼? – BrokenGlass

-1
  1. 看看調用堆棧或異常的堆棧跟蹤。
  2. 確定循環。
  3. 找出如何打破循環。
2

您的get方法調用本身:BoundingSphere調用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; 
    } 
} 

當您使用以下形式:

public BoundingSphere BoundingSphere { get; set } 

你不需要指定變量來存儲實際值,但是當你實現獲取或設置明確的,你應該聲明額外的變量,並用它獲取,設置實現。

0

我認爲你需要改變

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

這個

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