. Net using Com components

Source: Internet
Author: User
Some time ago. when the Supermap component (activeX) is used in net, it is often found that the memory cannot be written. Later, it contacted the super map company to say that the Com object needs to be released in the code.
Today, when using the MapX component, we found another strange problem: NullReferenceException occurs after processing part of the data during topology processing. The Code is as follows:

For (int I = 1; I <= axMap1.Layers [LayerName]. AllFeatures. Count; I ++)
{
MapXLib. Feature ft = axMap1.Layers [LayerName]. AllFeatures [I];
X1 = ft. Parts [1]. get_X (1 );
Y1 = ft. Parts [1]. get_Y (1 );
ProcessData (x1, y1 );
}
After checking for half a day, I did not find the cause. Later I thought it may be related to the release of the Com object, so I adjusted the code:
MapXLib. Layer lay = axMap1.Layers [LayerName];
MapXLib. Features fs = lay. AllFeatures;
Int c = fs. Count;
For (int I = 1; I <= c; I ++)
{
MapXLib. Feature ft = fs. _ Item (I );
MapXLib. Parts pts = ft. Parts;
MapXLib. Points pss = pts [1];
X1 = pss. get_X (1 );

Y1 = pss. get_Y (1 );

ProcessData (x1, y1 );
System. Runtime. InteropServices. Marshal. ReleaseComObject (pss );
Pss = null;
System. Runtime. InteropServices. Marshal. ReleaseComObject (pts );

Pts = null;
System. Runtime. InteropServices. Marshal. ReleaseComObject (ft );

Ft = null;
}
System. Runtime. InteropServices. Marshal. ReleaseComObject (fs );
Fs = null;
System. Runtime. InteropServices. Marshal. ReleaseComObject (lay );
Lay = null;

The adjusted code runs normally.

Several notes are found during the adjustment:
1. As long as the Com object is defined, it must be released after use. Otherwise, problems will occur after multiple calls.
2. Temporary variables also need to be released. For example
X1 = ft. Parts [1]. get_X (1 );
Discount:
MapXLib. Parts pts = ft. Parts;

MapXLib. Points pss = pts [1];

X1 = pss. get_X (1 );

Y1 = pss. get_Y (1 );
Release the variable.
3. If a method returns a Com Object, define the returned variable and release it.

In addition, it is found that there are some problems in browsing and editing blogs in the blog circle when using the FireFox browser.

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.