1. 程式人生 > >arcengine 開發幾個注意釋放記憶體的地方

arcengine 開發幾個注意釋放記憶體的地方

在迴圈、查詢過程中經常出現如下錯誤: 


1 、迴圈的 遊標一定要釋放不然會報錯。

 private void find_allroads_lxbm(IFeature feature_ld, string lxbm)
        {
            try
            {
                ISpatialFilter spatialfilter = new SpatialFilterClass();
                spatialfilter.WhereClause = "[LXBM] = " + lxbm;
                spatialfilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                //feature_ld該路段 feature_last下一路段
                while (feature_ld != null)
                {
                    IPolyline polyline = feature_ld.Shape as IPolyline;
                    //緩衝
                    IPoint end_pt = polyline.ToPoint;
                    ITopologicalOperator topo2 = end_pt as ITopologicalOperator;
                    IGeometry geo_buffer_end = topo2.Buffer(1);
                    spatialfilter.Geometry = geo_buffer_end;
                    IFeatureCursor featcursor_lastld = m_featcls.Search(spatialfilter, true);
                    IFeature feature_last = featcursor_lastld.NextFeature();
                    try
                    {
                        while (feature_last != null)
                        {
                         
                           /****省略***/
                                                 
                        }
                    }
                    catch (Exception exx)
                    {
                        
                        MessageBox.Show(exx.ToString());
                    }
                  
                    //下一個
                    feature_ld = feature_last;
                   
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(featcursor_lastld);
                   
                }


                System.Runtime.InteropServices.Marshal.ReleaseComObject(spatialfilter);
                ////垃圾回收
                System.GC.Collect();
                System.GC.WaitForPendingFinalizers();
            }
            catch (Exception exx)
            {

                MessageBox.Show(exx.ToString());
            }           
        }

2 關於查詢(ISpatialFilter,IQueryFilter):

 //該路線編碼路段數量
            IQueryFilter queryfilterall = new QueryFilterClass();
            queryfilterall.WhereClause = " [LXBM]= "+ lxbm;
            LDBM_MAXCOUNT = m_featcls.FeatureCount(queryfilterall);
            //起始值
            start_index = Convert.ToInt32(start_lxh);
            IQueryFilter queryfilter = new QueryFilterClass();
            queryfilter.WhereClause = " [LDBM]= "+ start_ldbm;
            IFeatureCursor featcursor = m_featcls.Search(queryfilter, true);//最好是true

參考:http://www.cnblogs.com/gisoracle/archive/2012/01/28/2330541.html

為true分析:pFeatures集合儲存了指向FeatureClass上所有Feature的引用,但是由於Recycling引數設定為TRUE也就是說每執行一個NextFeautre方法上一條記錄的Feature值在記憶體中被回收,所以到最後遍歷pFeatures集合的時候所有的IFeature引用指向的Feature物件都為Null。

未false分析:分析:pFeatures集合儲存了指向FeatureClass上所有Feature的引用,但是由於Recycling引數設定為False也就是說每執行一個NextFeautre方法上一條記錄的Feature值在記憶體中依然存在,所以到最後遍歷pFeatures集合的時候所有的IFeature引用指向的Feature物件都依然存在。所以會執行的很Happy(如下圖所示):