[Post] release of several common unmanaged objects in. NET + AE Development

Source: Internet
Author: User
[Post] release of some common unmanaged objects in. NET + AE Development
We know that. NET provides two methods for memory management: managed object management and non-managed object management. The memory management we usually understand is GC (garbage collection), although GC passes through
The management of managed stacks gives us the opportunity to free up from the problems of traditional locks such as memory leaks and focus on the logic of the program. However, handing over everything to the GC sometimes compromises the program.
Or even cause errors.


Why is this happening? The problem is that for non-hosted resources (file handles) or bitmap objects that require special attention, GC performance is a little unsatisfactory (this sentence or
Xu is not correct, because Microsoft designed GC to manage managed objects ). How to effectively use GC for memory management and how to optimize program performance is not covered in this article.
The purpose of this article is how to release unmanaged objects during AE development.

1. aouninitialize. Shutdown

This is often the case when we exit the AE application: "The instruction x
References memory at X. The memory cocould not be
Read ". The main cause of this error is that the COM Object (an unmanaged object) is not released. When we end the process that uses it, it stops us from releasing it normally. The priority of its release is higher than when
The priority of the process that uses it before release. That is to say, before the process is released, it must be released first.


After understanding the above principles, the solution to this problem is very simple. We only need to call it before the application exits.
ESRI. ArcGIS. ADF. comsupport. aouninitialize. Shutdown () method to release unmanaged objects (versions earlier than 9.2)
This aouninitialize is not in the namespace of ESRI. ArcGIS. ADF. comsupport. Note that ).

Ii. Marshal. releasecomobject

NET development, the reference COM object is mainly through RCW (runtime callable
Wrappers ). GC is a little powerless for the release of COM objects. Therefore, the resources occupied by COM objects must be released in the program.
Otherwise, unexpected errors may occur. For example, you can repeatedly open geodatabase from the personal Geodatabase.
Cursors, but not released in time, will cause "no more tables can be
Opened. "In other cases, you may find that the COM object is still referenced in the memory when the application exits. For example, if stylegallery is not displayed, exit the application
Will cause an error.

1. Releasing stylegallery:

Copy content to clipboard

Code:

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

2. Releasing Geodatabase cursors

Copy content to clipboard

Code:


for (int i = 1; i < 2500; i++)
{
      IQueryFilter qu = New QueryFilterClass();
       qu.WhereClause = @"Area = " + i.ToString();
      IFeatureCursor featCursor = featClass.Search(qu, true);
      // Use the feature cursor as required
      System.Runtime.InteropServices.Marshal.ReleaseComObject(featCursor);
  }

3. Releasing webobject (ArcGIS Server)

Copy content to clipboard

Code:

private void doSomthing_Click(object sender, System.EventArgs e)
{
using (WebObject webobj = new WebObject())
{
    ServerConnection serverConn = new ServerConnection("doug", true);
    IServerObjectManager som = serverConn.ServerObjectManager;
    IServerContext ctx = som.CreateServerContext("Yellowstone","MapServer");
    IMapServer mapsrv = ctx.ServerObject as IMapServer;
    IMapServerObjects mapo = mapsrv as IMapServerObjects;
    IMap map = mapo.get_Map(mapsrv.DefaultMapName);
    IFeatureLayer flayer = map.get_Layer(0) as IFeatureLayer;
    IFeatureClass fclass = flayer.FeatureClass;
    IFeatureCursor fcursor = fclass.Search(null, true);
    webobj.ManageLifetime(fcursor);
    IFeature f = null;
    while ((f = fcursor.NextFeature()) != null)
    {
      // do something with the feature
    }
    ctx.ReleaseContext();
}
}

3. When should I release an unmanaged object?

This issue is very important. When we are sure that we will not call unmanaged object Resources after release, we can call system. runtime. interopservices. marshal. the releasecomobject () method is explicitly released. Otherwise, a new error is thrown.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.