[vtkusers] vtkdotnet accessviolation exceptions

Donny Zimmerman dmzimm at charter.net
Thu Apr 29 23:18:51 EDT 2010


I am not sure if anyone is using the vtkdotnet wrappers, but I am having a
terrible problem with accessviolation exceptions. Mostly when closing the
application, but sometimes while the app is running. There is no pattern to
it, it is completely random. I have several classes that create mappers,
planes,  polydata, and actors which are private class members so they stay
alive as long as the class instance is alive and the scene is getting
rendered exactly like I want. When the main form is closing I call a
DestroyLayer function on each layer object which sets all the vtk objects
instances in the class to null.  When creating the actors I pass a reference
to the renderer so I can add the actor to the renderer. The stack trace
usually points to the Finalize() function for vtkPlane or vtkMapper when the
exception occurs and the exception occurs at the very end of the application
life cycle ( after the call to Application.Run() returns in the static
Main() function. I am thinking about using System.GC.SuppressFinalize(), but
I don't know if this is the right approach.  Here is a code snippet of one
of my classes:

 

class vtkNex3DMapCountyLinesLayer : Nex3DVtkLayer

    {

        

        #region Class Properties

        

        protected Color m_linecolor;

        public Color LineColor

        {

            get { return m_linecolor; }

            set { m_linecolor = value; }

        }

 

        protected double m_linetransparency;

        public double LineTransparency

        {

            get { return m_linetransparency; }

            set { m_linetransparency = value; }

        }

 

        protected float m_linewidth;

 

        public float LineWidth

        {

            get { return m_linewidth; }

            set { m_linewidth = value; }

        }        

 

        #endregion        

 

        vtkActor m_actor;

        vtkPoints m_points;

        vtkCellArray m_line_cells;

        vtkPolyData m_polylines;

        vtkPolyDataMapper m_polymapper;

        vtkPlaneCollection m_planecollection;

        vtkPlane m_plane;

 

        public vtkNex3DMapCountyLinesLayer(Nex3DAppInterface ai)

            : base(ai)

        {            

            m_linetransparency = 120.0;

            m_linecolor = Color.Orange;

            m_linewidth = 1.25f;                     

            m_clipdistance = 7.0;

 

            m_actor = null;

            m_points = null;

            m_line_cells = null;

            m_polylines = null;

            m_polymapper = null;

            m_planecollection = null;

            m_plane = null;

        }

 

        public override void UpdateClipPlane(double[] normal, double[]
origin, double[] frustum, vtkRenderer ren)

        {

            try

            {

                if (null != m_actor)

                {

                    if (null != m_actor.GetMapper())

                    {

                        if (null != m_actor.GetMapper().GetClippingPlanes())

                        {

                            if
(m_actor.GetMapper().GetClippingPlanes().GetNumberOfItems() > 0)

                            {

 
m_actor.GetMapper().GetClippingPlanes().InitTraversal();

 
m_actor.GetMapper().GetClippingPlanes().GetNextItem().SetNormal(normal);

 
m_actor.GetMapper().GetClippingPlanes().InitTraversal();

 
m_actor.GetMapper().GetClippingPlanes().GetNextItem().SetOrigin(origin);

 
m_actor.GetMapper().GetClippingPlanes().InitTraversal();

 
m_actor.GetMapper().GetClippingPlanes().GetNextItem().Push(-m_clipdistance);

                            }

                        }

                    }

                }

            }

            catch

            {

                RaiseEvent("Error Setting Clipping Plane For: " +
m_layername, 0);

            }

            

 

        }

 

        public override void DestroyLayer(vtkRenderer ren)

        {

            try

            {         

 

                if (null != m_polymapper)

                {

                    m_polymapper.RemoveAllInputs();                    

                }

 

                if (null != m_polylines)

                {

                    m_polylines.Reset();                    

                }

 

                if (null != m_points)

                {

                    m_points.Reset();                    

                }

 

                if (null != m_line_cells)

                {

                    m_line_cells.Reset();                   

                }

 

                if (null != m_planecollection)

                {

                    m_planecollection.RemoveAllItems();                   

                }

 

                if (null != m_actor)

                {

                    ren.RemoveActor(m_actor);

 
//m_actor.ReleaseGraphicsResources(ren.GetRenderWindow());


                }

 

                

                m_actor = null;               

                m_points = null;                

                m_line_cells = null;                

                m_polylines = null;                

                m_polymapper = null;                

                m_planecollection = null;                

                m_plane = null;

 

                

                

            }

            catch

            {

                RaiseEvent("Error Destroying Actor: " + m_layername, 0);

            }

        }

 

        public override bool RefreshData(vtkRenderer ren)

        {

            try

            {

                //This will clear the actor and resources

                DestroyLayer(ren);

 

                List<float> verts = new List<float>();

                List<int> vert_counts = new List<int>();                

                

                List<string> filenums = get_map_tile_names();

 

                string path = AppDomain.CurrentDomain.BaseDirectory;

                path += @"Data\mapareas\CO\map.cmf";

 

                using (ZipFile zf = new ZipFile(path))

                {

 

                    foreach (string filenum in filenums)

                    {

                        CountyObjects co = new CountyObjects();

 

                        ZipEntry ze = zf["map" + filenum];

 

                        using (MemoryStream ms = new MemoryStream())

                        {

                            ze.Extract(ms);

                            ms.Seek(0, SeekOrigin.Begin);

                            IFormatter formatter = null;

                            formatter = new BinaryFormatter();

                            co = (CountyObjects)formatter.Deserialize(ms);

                        }

 

                        foreach (List<float> cl in co.counties)

                        {

                            verts.AddRange(cl);

                            vert_counts.Add(cl.Count / 3);

 

                        }

 

                    }

                }

 

                build_lines(verts, vert_counts);

                ren.AddActor(m_actor);

 

            }

            catch(Exception e)

            {

                RaiseEvent("Error Creating County Lines Layer: " +
e.Message, 0);

                return false;

            }

 

            m_appinterface.FirstSiteLoad = false;

 

            return true;

 

        }        

 

        #region private methods

        

 

        private void build_lines(List<float> verts, List<int> lines)

        {

 

            try

            {

                m_points = new vtkPoints();

 

                for (int v = 0; v < verts.Count; v += 3)

                {

                    m_points.InsertNextPoint(verts[v], verts[v + 1], verts[v
+ 2]);

                }

 

                m_line_cells = new vtkCellArray();

                int index = 0;

                foreach (int cnt in lines)

                {

                    int[] connectivity = new int[cnt];

                    for (int i = 0; i < cnt; i++)

                    {

                        connectivity[i] = index++;

                    }

                    m_line_cells.InsertNextCell(connectivity.Length,
connectivity);

 

                }

 

                m_polylines = new vtkPolyData();

 

                m_polylines.SetPoints(m_points);

                m_polylines.SetLines(m_line_cells);

                m_polylines.Update();

 

                m_polymapper = new vtkPolyDataMapper();

                m_planecollection = new vtkPlaneCollection();

                m_plane = new vtkPlane();

                m_plane.SetNormal(0.0, 0.0, 1.0);

                m_plane.SetOrigin(m_appinterface.SiteData.CenterLon,
m_appinterface.SiteData.CenterLat, 0.0);

                m_planecollection.AddItem(m_plane);

                m_polymapper.SetClippingPlanes(m_planecollection);

                m_polymapper.SetInput(m_polylines);

                m_actor = new vtkActor();

                m_actor.SetMapper(m_polymapper);

                m_actor.AddPosition(0, 0, 0);

                m_actor.GetProperty().SetDiffuseColor((double)m_linecolor.R
/ 255.0,

                    (double)m_linecolor.G / 255.0, (double)m_linecolor.B /
255.0);

                m_actor.GetProperty().SetOpacity((double)(m_linetransparency
/ 255.0));

 

                m_actor.GetProperty().SetLineWidth(m_linewidth);

            }

            catch

            {

                throw;

            }

 

 

        }

 

        #endregion

 

    }

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100429/940d98ab/attachment.htm>


More information about the vtkusers mailing list