Try to read or write the protected memory. This usually indicates that other memory is damaged.

Source: Internet
Author: User
Net + AE development in the release of several common unmanaged objects from: http://hi.baidu.com/murphy1314

Release of several 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 manages managed heaps, this 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 may sometimes compromise the program's efficiency, or even cause errors.

Why is this happening? The problem is that the GC performance is a little unsatisfactory for unmanaged resources (file handles) or bitmap objects that require special attention (this sentence may not be 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 release is higher than that of the process currently using it. That is to say, before a 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 ESRI before the application exits. arcGIS. ADF. comsupport. aouninitialize. the shutdown () method is used to release the unmanaged object. (versions earlier than 9.2 of aouninitialize are not in ESRI. arcGIS. ADF. in the comsupport namespace, 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
Opening Geodatabase cursors in Geodatabase but not releasing them 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 does not show release, an error is thrown when the application exits.

1. Releasing stylegallery:

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

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)

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
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.