[C#] DirectX AccesViolationException (Buffer)

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von razorblade, 8. Juni 2010 .

Schlagworte:
Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 8. Juni 2010
    DirectX AccesViolationException (Buffer)

    Hallo Leute,

    bin gerade dabei eine Wellensimulation in C# zu programmieren. Die Darstellung erfolgt mit Hilfe von DirectX.

    Ich habe mir einen VertexBuffer erstellt und das ganze funktioniert eigtl. auch ganz ordentlich...

    Nur nach einer Weile schmeist er mir immer eine System.AccesViolationException.

    Hier der Code (ohne Wellenberechnung):

    Spoiler
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    
    namespace WaveSim
    {
     public partial class Form1 : Form
     {
     
     Functions fc = new Functions(); //instantiate the object for all required functions
     private Device dv; //DirectX device
     private CustomVertex.PositionColored[] vertices; //variable vertices
     static VertexBuffer vBuffer;
     
    
     
     public Form1()
     {
     InitializeComponent();
     }
    
     private void buttonBeenden_Click(object sender, EventArgs e)
     {
     Close(); //exit programm
     }
    
     private void buttonStart_Click(object sender, EventArgs e)
     {
     fc.Time = 0; 
     timer.Enabled = true;
     initGraphics(); //initialize graphics
     }
    
     private void buttonStop_Click(object sender, EventArgs e)
     {
     timer.Enabled = false;
     fc.GridAcceleration = fc.resetArray(fc.GridAcceleration); //reset all array to 0
     fc.GridSpeed = fc.resetArray(fc.GridSpeed);
     fc.GridZ = fc.resetArray(fc.GridZ);
     eraseDevice(); //erase 3D device from memory
     }
    
     private void trackBarFrequency_Scroll(object sender, EventArgs e)
     {
     fc.Periode = (1.0 / (trackBarFrequency.Value/100.0)); //Periode T = 1 / frequency
     showStatusInfo(); //show infos in status bar
     }
    
     private void trackBarAmplitude_Scroll(object sender, EventArgs e)
     {
     fc.Amplitude = trackBarAmplitude.Value; //assign the amplification from trackBarAmplitude
     showStatusInfo(); //show infos in status bar
     }
    
     private void trackBarDamping_Scroll(object sender, EventArgs e)
     {
     fc.Damping = trackBarDamping.Value / 1000.0d; //assign the damping from trackBarDamping
     showStatusInfo(); //show infos in status bar
     }
    
     private void Form1_Load(object sender, EventArgs e)
     {
     fc.Periode = 1.0d; //Default start with T = 1
     fc.Time = 0; //start time t=0
     timer.Interval = (int)( 1 * fc.Periode);
     fc.generateArrays();
     trackBarAmplitude.Value = (int)fc.Amplitude;
     labelX.Text = "X= " + fc.DimensionX.ToString();
     trackBarDimX.Value = fc.DimensionX;
    
     labelY.Text = "Y= " + fc.DimensionY.ToString();
     trackBarDimY.Value = fc.DimensionY;
     showStatusInfo(); //show infos in status bar
     }
    
     public void showStatusInfo()
     {
     //show actual values in the toolStrip at the bottom
     toolStripStatusLabelAmplitude.Text = "Amplitude: " + (fc.Amplitude).ToString();
     toolStripStatusLabelDamping.Text = "Dämpfungsfaktor: " + fc.Damping.ToString();
     toolStripStatusLabelFrequency.Text = "Frequenz: " + (1.0/fc.Periode).ToString() + "Hz";
     }
    
     private void timer_Tick(object sender, EventArgs e)
     {
     //wave(fc.Time); //create wave at time t
     fc.Time+=0.09f; //t = t + 1
     fc.calcWave(fc.Time);
     render();
     }
    
     private void trackBarLambda_Scroll(object sender, EventArgs e)
     {
     fc.Lambda = trackBarLambda.Value; //assign wavelength
     }
    
     public void initGraphics()
     {
     try
     {
     panelSimulation.ClientSize = new Size(600, 600);
     PresentParameters pp = new PresentParameters();
     pp.Windowed = true;
     pp.SwapEffect = SwapEffect.Copy;
     dv = new Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware, panelSimulation, CreateFlags.HardwareVertexProcessing, pp);
    
     }
     catch (DirectXException e)
     {
     timer.Stop();
     MessageBox.Show(e.ToString());
     }
     }
    
     public void render()
     {
     dv.Transform.Projection = Matrix.PerspectiveFovLH(1f, 1, 1f, 10000f);
     dv.Transform.View = Matrix.LookAtLH(new Vector3((1000), (1000), 500), new Vector3(fc.DimensionX, fc.DimensionY, 0), new Vector3(0, 0, 1));
     dv.RenderState.Lighting = false;
    
     vertices = new CustomVertex.PositionColored[fc.DimensionX*fc.DimensionY];
    
     int i = 0;
    
     for (int x = 1; x < fc.DimensionX; x++)
     {
     for (int y = 1; y < fc.DimensionY; y++)
     {
     vertices[i].Position = new Vector3((float)x * 15, (float)y * 15, (float)fc.GridZ[x, y]);
     vertices[i].Color = Color.Green.ToArgb(); 
     i++;
     }
     }
     dv.VertexFormat = CustomVertex.PositionColored.Format;
     try
     {
     vBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
     360,
     dv,
     Usage.WriteOnly,
     CustomVertex.PositionColored.Format,
     Pool.Default);
     vBuffer.SetData(vertices, 0, LockFlags.None);
     }
     catch (AccessViolationException e)
     {
     timer.Stop();
     MessageBox.Show(e.ToString());
     }
     dv.SetStreamSource(0, vBuffer, 0);
     dv.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
     dv.BeginScene();
     dv.DrawUserPrimitives(PrimitiveType.PointList, i, vertices); //draw 1 dot
     dv.EndScene();
     dv.Present();
     }
    
    
     public void eraseDevice()
     {
     dv.Dispose(); //delete Device
     }
    
     private void chkBorder_CheckedChanged(object sender, EventArgs e)
     {
     fc.FixedEdges = !fc.FixedEdges;
     }
    
     private void chkDouble_CheckedChanged(object sender, EventArgs e)
     {
     fc.DoubleSlit = !fc.DoubleSlit;
     }
    
     private void trackBarDimX_ValueChanged(object sender, EventArgs e)
     {
     fc.DimensionX = trackBarDimX.Value;
     fc.generateArrays();
     labelX.Text = "X= " + fc.DimensionX;
     }
    
     private void trackBarDimY_Scroll(object sender, EventArgs e)
     {
     fc.DimensionY = trackBarDimY.Value;
     fc.generateArrays();
     labelY.Text = "Y= " + fc.DimensionY;
     }
    
     }
    }
     
  2. 17. Juni 2010
    AW: DirectX AccesViolationException (Buffer)

    Hab den Fehler selbst gefunden...

    Ich hatte vergessen die größe des Buffers anzupassen wenn ich meine Membran geändert habe *kopf@tisch*

    CLOSED
     
  3. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.