treelight

memory leak in mapcontrol

Discussion created by treelight on May 15, 2011
Latest reply on May 18, 2011 by treelight
Hello all, I'm using dynamic mode to draw polylines in the mapcontrol, the codes are as follows. After running the program, the lines are drawn, but I found the memory was increasing all the time, anyone please help me to find the issue? thanks!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;

namespace MapMemTest
{
    public partial class Form1 : Form
    {
        IMapControl4 m_mapControl = null;
        Timer stoptimer = new Timer();
        private IMap m_map;
        private IActiveView m_activeView;
        private IDisplay m_display;

        private IDynamicMap m_dynamicMap = null;
        private bool m_bIsDynamicMode = false;
        protected object Missing = Type.Missing;

        public Form1()
        {
            InitializeComponent();

            Init();
        }

        void Init()
        {
            m_mapControl = axMapControl1.Object as IMapControl4;
            m_mapControl.Extent = m_mapControl.FullExtent;
          
            m_activeView = m_mapControl.Map as IActiveView;
            m_dynamicMap = m_mapControl.Map as IDynamicMap;
            if (!m_bIsDynamicMode)
            {
                //switch into dynamic mode
                if (!m_dynamicMap.DynamicMapEnabled)
                    m_dynamicMap.DynamicMapEnabled = true;

                //start listening to DynamicMap's 'After Draw' events
                ((IDynamicMapEvents_Event)m_dynamicMap).AfterDynamicDraw +=
                    new IDynamicMapEvents_AfterDynamicDrawEventHandler(Form1_AfterDynamicDraw);
            }

            stoptimer.Enabled = true;
            stoptimer.Interval = 500;
            stoptimer.Tick += new EventHandler(stoptimer_Tick);
        }

        void Form1_AfterDynamicDraw(esriDynamicMapDrawPhase DynamicMapDrawPhase, IDisplay Display, IDynamicDisplay dynamicDisplay)
        {
            if (esriDynamicMapDrawPhase.esriDMDPDynamicLayers != DynamicMapDrawPhase)
                return;
            DrawLineMemTest(dynamicDisplay);

        }

        void DrawLineMemTest(IDynamicDisplay dynamicDisplay)
        {
            IPointCollection4 pointcoll0 = new PolylineClass();
            IPoint p0 = new PointClass();
         
            List<IPoint> tmppoints = new List<IPoint>();           
            int pointcount = 1801;
            WKSPoint[] points = new WKSPoint[pointcount];

            int tmpcount = 0;
            for (double i = -90; i < 90; i += 0.1)
            {
                WKSPoint wksp;
                wksp.X = i;
                wksp.Y =i;

                points[tmpcount] = wksp;
                tmpcount++;
            }

            IGeometryBridge2 pGeoBri = new GeometryEnvironmentClass();
            pGeoBri.AddWKSPoints(pointcoll0,ref points);

            DynamicDrawLine(pointcoll0, 230, 0, 0, 2, dynamicDisplay);
          
        }

        void stoptimer_Tick(object sender, EventArgs e)
        {
           m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
        }

        void DynamicDrawLine(IPointCollection linePointColl, byte r, byte g, byte b,
          double width, IDynamicDisplay dynamicDisplay)
        {
            m_dynamicSymbolProps = dynamicDisplay as IDynamicSymbolProperties2;
            m_dynamicSymbolProps.SetScale(esriDynamicSymbolType.esriDSymbolLine, (float)width, (float)width);
            m_dynamicSymbolProps.SetColor(esriDynamicSymbolType.esriDSymbolLine, (float)r / 255, (float)g / 255, (float)b / 255, 1.0f);
         
            dynamicDisplay.DrawPolyline(linePointColl);
        }
    }
}

Outcomes