Excited! Found the method for releasing the iterator

Source: Internet
Author: User
ArticleDirectory
    • Sample Code
    • Return Value

The read operation of VSS information has a job that affects performance. I am not looking for the number of lines to locate it. Let's take a look. Like this:
While (enumerator. movenext ())
{
/// Empty Loop
}
This problem has plagued me for a long time. This morning I was bored and began to look for materials and read many places. Some foreigners seem to have encountered this problem. They proposed on the discussion board. The solution is the one I wrote above. The next historical iteration can be performed only after enumerator. movenext () returns false.
Otherwise, you will see

An unhandled exception of Type 'System. runtime. interopservices. comexception' occurred in vssinfo.exe.

Additional information: A History operation is already in progress.

My first instinct is that the COM object is not released.
If you use system. runtime. interopservices. marshal. releasecomobject (enumerator) obviously reports an error because the iterator is generated by com InterOP. is not a direct proxy for the COM interface.

The following is an example of running VSS in C ++ on msdn.

 

Sample Code

# Include <windows. h> # include <ocidl. h> # include <stdio. h> # include "ssauto. H "Void listversions (ivssdatabase * dB, lpcstr path) {BSTR bstrval; char lpbuf [200]; char lpbuf2 [200]; ivssitem * vssi; ivssversion * vers; ivssversions * VX; lpunknown lpunk; ienumvariant * ppvobj; variant st; olechar * svalue; BSTR bstrvalue; int X; ulong fetched; long lvnum; If (x = multibytetowidechar (cp_acp, 0, path, -1, svalue, 0 ))! = 1) {svalue = new olechar [X]; If (multibytetowidechar (cp_acp, 0, path,-1, svalue, x) = 0) MessageBox (null, "Error in conversion", "multibytetowide", mb_ OK);} elsesvalue = l "; bstrvalue = sysallocstring (svalue); If (s_ OK = DB-> get_vssitem (bstrvalue, false, & vssi) {If (s_ OK = vssi-> get_versions (0l, & VX) {If (s_ OK = VX-> _ newenum (& lpunk )) {If (! Failed (lpunk-> QueryInterface (iid_ienumvariant, (void **) & ppvobj) {vssi-> get_spec (& bstrval); X = widechartomultibyte (cp_acp, 0, (lpcwstr) bstrval,-1, lpbuf, sizeof (lpbuf), null, null); printf ("History of: % s \ n", lpbuf ); printf ("Action user name version number \ n"); do {ppvobj-> next (1ul, & St, & fetched); If (fetched! = 0) {If (! Failed (St. punkval-> QueryInterface (iid_ivssversion, (void **) & vers) {vers-> get_action (& bstrval); widechartomultibyte (cp_acp, 0, (lpcwstr) bstrval, -1, lpbuf, sizeof (lpbuf), null, null); vers-> get_username (& bstrval); widechartomultibyte (cp_acp, 0, (lpcwstr) bstrval,-1, lpbuf2, sizeof (lpbuf2), null, null); vers-> get_versionnumber (& lvnum); printf ("% S % LD \ n", lpbuf, lpbuf2, lvnum ); vers-> release () ;} St. punkval-> release () ;}} while (fetched! = 0 );Ppvobj-> release (); } Lpunk-> release ();} VX-> release ();} vssi-> release ();} sysfreestring (bstrvalue );}

C ++ follows the release rule when operating on com, But how should the iterator be released?

 
 

 
Marshal. releasecomobject (icustomadapter) enumerator). getunderlyingobject ());
Marshal. releasethreadcache ();



Provides access permissions for the basic object encapsulated by the custom mail collector.

 

[C #]
Object getunderlyingobject ();

 

Return Value

The object contained in the adapter object.

Today's mood is getting better ......

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.