jimstapleton's picture

AccessViolationException

First off, I think my issue is about the same as Here.

However, being such a noob to the library, I'm afraid I can't translate the issue to work on my problem. The bolded line is what is throwing the error.

Cliff's notes code (full code below - this should be the necessary bits)

namespace ExperimentalApp1
{
    public class ExperimentalGLWindow : AOGLWindow
    {
        protected Vector4[] m_vertices = null;
        protected OpenTK.Graphics.Color4[] m_primary_colors = null;
        protected Vector3[] m_normals = null;
        protected int[,] m_indices = null;
        protected ControlForm m_window;
        protected List<Tuple<string, Thread>> tl;
 
         /*...*/
 
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            tl.Add(new Tuple<string, Thread>("OnRenderFrame", Thread.CurrentThread));
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.VertexPointer<Vector4>(m_vertices.Length, VertexPointerType.Float,
                                        BlittableValueType.StrideOf<Vector4>(m_vertices), m_vertices);
            GL.ColorPointer<OpenTK.Graphics.Color4>(m_primary_colors.Length, ColorPointerType.Float,
                                        BlittableValueType.StrideOf<OpenTK.Graphics.Color4>(m_primary_colors), m_primary_colors);
            GL.NormalPointer<Vector3>(NormalPointerType.Float,
                                          BlittableValueType.StrideOf<Vector3>(m_normals), m_normals);
            GL.IndexPointer<int>(IndexPointerType.Int, BlittableValueType.StrideOf<int>(m_indices),
                m_indices);
 
            GL.Begin(BeginMode.Quads);
            <B>GL.ArrayElement(0);</B>
            GL.ArrayElement(1);
            GL.ArrayElement(2);
            GL.ArrayElement(3);
            GL.End();
 
            SwapBuffers();
        }
    }
}

Full Code

namespace ExperimentalApp1
{
    public class ExperimentalGLWindow : AOGLWindow
    {
        protected Vector4[] m_vertices = null;
        protected OpenTK.Graphics.Color4[] m_primary_colors = null;
        protected Vector3[] m_normals = null;
        protected int[,] m_indices = null;
        protected ControlForm m_window;
        protected List<Tuple<string, Thread>> tl;
 
        public ExperimentalGLWindow(ControlForm window)
            : base()
        {
            base.init();
 
            tl = new List<Tuple<string, Thread>>();
            tl.Add(new Tuple<string, Thread>("CONSTRUCTOR", Thread.CurrentThread));
            m_window = window;
 
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.EnableClientState(ArrayCap.NormalArray);
            GL.EnableClientState(ArrayCap.IndexArray);
 
            m_vertices = new Vector4[] {
                new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(0.25f, 1.0f, 0.0f, 1.0f),
                new Vector4(0.25f, 0.25f, 0.0f, 1.0f),
                new Vector4(1.0f, 0.25f, 0.0f, 1.0f),
                new Vector4(-1.0f, -1.0f, 0.0f, 1.0f),
                new Vector4(-0.25f, -1.0f, 0.0f, 1.0f),
                new Vector4(-0.25f, -0.25f, 0.0f, 1.0f),
                new Vector4(-1.0f, -0.25f, 0.0f, 1.0f)
            };
 
            m_primary_colors = new OpenTK.Graphics.Color4[] {
                new OpenTK.Graphics.Color4(1.0f,0.0f,0.0f,1.0f),
                new OpenTK.Graphics.Color4(0.0f,1.0f,0.0f,1.0f),
                new OpenTK.Graphics.Color4(0.0f,0.0f,1.0f,1.0f),
                new OpenTK.Graphics.Color4(1.0f,1.0f,1.0f,1.0f),
                new OpenTK.Graphics.Color4(1.0f,0.0f,0.0f,1.0f),
                new OpenTK.Graphics.Color4(0.0f,1.0f,0.0f,1.0f),
                new OpenTK.Graphics.Color4(0.0f,0.0f,1.0f,1.0f),
                new OpenTK.Graphics.Color4(1.0f,1.0f,1.0f,1.0f)
            };
 
            m_normals = new Vector3[] {
                new Vector3(0.0f,0.0f,1.0f),
                new Vector3(0.0f,0.0f,1.0f),
                new Vector3(0.0f,0.0f,1.0f),
                new Vector3(0.0f,0.0f,1.0f),
                new Vector3(0.0f,0.0f,1.0f),
                new Vector3(0.0f,0.0f,1.0f),
                new Vector3(0.0f,0.0f,1.0f),
                new Vector3(0.0f,0.0f,1.0f)
            };
 
            m_indices = new int[,] {
                {0,1,2,3},
                {4,5,6,7}
            };
        }
 
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
 
            if(m_window.IsAlive) m_window.Close();
        }
 
        /// <summary>
        /// Main initialization (loading) routine)
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            tl.Add(new Tuple<string, Thread>("OnLoad", Thread.CurrentThread));
            m_active = true;
            //set the window area
            GL.Viewport(0, 0, 400, 400);
            //background color
            GL.ClearColor(Color.Black);
            //set the view area
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(-2, 2, -2, 2, 2, -2);
            //now back to 'scene editing' mode
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            //make things look nice
            GL.ShadeModel(m_shading_model);
 
            //set up our z-rendering logic
            GL.ClearDepth(2.0000f);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);
 
            //other improvements to quality
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
 
            base.OnLoad(e);
        }
 
        /// <summary>
        /// Frame drawing
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            tl.Add(new Tuple<string, Thread>("OnRenderFrame", Thread.CurrentThread));
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.VertexPointer<Vector4>(m_vertices.Length, VertexPointerType.Float,
                                        BlittableValueType.StrideOf<Vector4>(m_vertices), m_vertices);
            GL.ColorPointer<OpenTK.Graphics.Color4>(m_primary_colors.Length, ColorPointerType.Float,
                                        BlittableValueType.StrideOf<OpenTK.Graphics.Color4>(m_primary_colors), m_primary_colors);
            GL.NormalPointer<Vector3>(NormalPointerType.Float,
                                          BlittableValueType.StrideOf<Vector3>(m_normals), m_normals);
            GL.IndexPointer<int>(IndexPointerType.Int, BlittableValueType.StrideOf<int>(m_indices),
                m_indices);
 
            GL.Begin(BeginMode.Quads);
            <B>GL.ArrayElement(0);</B>
            GL.ArrayElement(1);
            GL.ArrayElement(2);
            GL.ArrayElement(3);
            GL.End();
 
            SwapBuffers();
        }
    }
}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Inertia's picture

I've been asked to post the question, because somehow the spam filter won't let it through O,o

"jimstapleton" wrote:

I've worked on it some more, and still can't fix the problem. Things I've tried:

1) I got rid of the Normal and Index pointer calls. I never added them back in.
2) I replaced BlittableValueType.StrideOf with 4 for the vertex/color calls (I know this isn't a useable value, but it should be safe, and then 16.
3) I replaced the Vector4 and Color4 arrays with 2D float arrays, the outer array containing 8 items, and each inner, 4. I then tried using both BlittableValueType.StrideOf, 4 and 16 in the pointer setup.

In all cases, I still get this error. The application is only running in one thread .

Can anyone suggest how I might be able to fix this, or what to look at?

We actually have a page in the book regarding that topic:
http://www.opentk.com/doc/graphics/geometry/vertex-arrays