ArcEngine Com objects in the release of skills

Statement 1: I Caishuxueqian, with Guo Degang words, "I was a schoolboy," if wrong, welcomed the discussion, do not abuse ^ _ ^.
Statement 2: Keep the original blog when reproduced the original link or add my blog address at the beginning of the article, or the right to retain legal liability.
Welcome plus group: GIS Development tribes

origin

AE development and Com objects often dealing with reasonable Com objects to the release of the stability program run fast. Com object is not released in time could lead to the following questions

  • High memory usage, in extreme cases lead to abnormal memory
  • Program exception
  • Data lock, such as table locks, lock the mosaic dataset, file locking and other issues
  • You can not delete a file (such as shapefile)
  • You can not delete FeatureClass, you can not modify table structure

This article summarizes how to release Com objects, as well as the release of Com object skills, learn how to release Com objects is an essential skill, to sum up, For Me And For You, I hope you can write code that is not despicable.

Common Com objects to be released

Cursor object (e.g. IFeautreCursor, ICursor), Geodatabase objects (e.g. IWorkspace, IDataset, IFeatureClass, ITable, IRaster, IRasterDataset, IMosaciDataset etc.), StyleGallery objects.

Several methods Com object of the release

Help ArcEngine reference position
Here Insert Picture Description

AOUninitialize.Shutdown

Here Insert Picture Description
Sometimes you develop a stand-alone program shut down will be reported when an unexpected error when you exit a program loaded MapControl of ArcEngine you may receive an error similar to the following, The instruction x references memory at x. The memory could not be read.
when the time reserved for the COM object in memory of more than It is expected that these errors may occur, thereby preventing the COM library unloaded from the process when the process is closed properly. To help prevent these errors, add ESRI.ArcGIS.ADF.Locala reference, add a static Shutdownfunction, which before the unloading process by ensuring close unused COM reference, help to avoid these errors.

ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown()

This function is not only to help unload the COM object libraries. After processing any COM object to which this function more useful. For example, in Arcgis Engine for Windows application with a startup form, the call will be placed aoUninitialize.shutdown event handler in the form of release.

ComReleaser

Was added before use ESRI.ArcGIS.ADF.Connection.Local.dll, a common method of managing comReleaser.ManageLifetime Com object, as a simple example code:

using (ComReleaser comReleaser = new ComReleaser())
{
    var comObj1=......;//伪代码
    comReleaser.ManageLifetime(comObj1);
    ......
    var comObj2=......;//伪代码
    comReleaser.ManageLifetime(comObj2);
    ......
}

Use ComReleaser Management Com object when Using statement is finished, that is all Com objects are released, the proposed code and objects Com object management put together, so either modify the code or checking code are relatively simple.

Marshal.ReleaseComObject

Needs to be added at the beginning of a reference code file type Using System.Runtime.InteropServices.Marshalgenerally used Marshal.ReleaseComObjectmethod to release Com objects, using the example below:

private void MyFunction()
{    
	ESRI.ArcGIS.Display.IStyleGallery styCls = new	ESRI.ArcGIS.Framework.StyleGalleryClass()as	ESRI.ArcGIS.Display.IStyleGallery;    // Use the StyleGalleryClass here.
    int refsLeft = 0;
    do
	{
        refsLeft = Marshal.ReleaseComObject(styCls);
	}
    while (refsLeft > 0);
}

Com objects release Notes

  1. Priority Use ComReleaserManagement Com Objects
  2. When using Com object, it is best to manage them, to avoid missing
  3. Com objects generated in the cycle, it is recommended timely recovery, do not wait cycle is complete a one-time recovery
  4. Function generated Com objects will be loosed avoid (such as the use of missing ISaveAstime interface to save the image)

Com object releases Common Errors

The following is the code for the actual development of various pits father I encountered in the project, read the following suggestions to avoid the tragic write code. (Ps: Sample Code The following are pseudo-code)

  • Release of the target object selection errors, such as the release of the Null Object

    Using(ComReleaser = new ComReleaser())
    {
    	IFeatureCursor pFeaCursor=pFeatureClass.Search(null,true);
    	//错误写法
    	IFeature pFeature = null;
    	pComReleaser.ManageLifetime(pFeature);//类似这种的代码,对Null对象上管理,基本无用
    	pFeature=pFeaCursor.NextFeature();
    	//正确写法
    	IFeature pFeature=pFeaCursor.NextFeature();
    	if(pFeature!=null)
    	{
    		pComReleaser.ManageLifetime(pFeature);//要管理非空的对象才有意义
    		......
    	}	
    }
    
  • Release times for the same object or global object (usually this problem in IWorkspaceFactory IWorkspace IFeatureClassthe more common, we suggest that you look at the concept of a singleton and ArcEngine resource pool)

    //定义一个IWorkspace对象(可以是全局对象或者单例对象,不懂单例的可以百度单例模式)
    IWorkspace pWs = pWsFactory.OpenWorkspace(wsPath,0);
    //用一个Workspace同时打开两次要素类,得到的其实是同一个要素类引用
    IFeatureClass pFeaClass1 = (pWs as IFeatureWorkspace).OpenFeatureClass(tableName);
    IFeatureClass pFeaClass2 = (pWs as IFeatureWorkspace).OpenFeatureClass(tableName);
    //释放其中的一个要素类
    Marshal.ReleaseComObject(pFeaClass1);
    //再次调用另外一个要素类会报错
    var name=(pFeaClass2 as IDataSet).Name;
    
  • Temporaries function generated neglect release

    IDataset pDataset=pSaveAs.Save(参数1,参数2,参数3);
    //切记要释放SaveAs产生的零食对象
    Marshal.ReleaseComObject(pDataset);
    
  • Com objects cycle should be released in time

    for(int i=0;i<10000;i++)
    {
    	IRow pRow=pTable.CreateRow();
    	......
    	//测试的Pow使用完毕记得释放
    	Marshal.ReleaseComObject(pRow);
    }
    

Exercise

  1. Use the cursor query data, which the management of Com object.
  2. Save raster data, temporary Com Object Management generated.
  3. Handwritten an open Shapefile, and then do a simple attribute query, Experience Management Com object.
Published 96 original articles · won praise 95 · views 390 000 +

Guess you like

Origin blog.csdn.net/yh0503/article/details/102796713