<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: What is the best practise when using ComReleaser? in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429062#M11612</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Is feature a managed or unmanaged object? &lt;BR /&gt;&lt;BR /&gt;If it is a managed object, wouldn't the fact that the previous feature reference make it eligible for garbage collection in .NET?&amp;nbsp; Otherwise, I agree, you should release it.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/ee787088.aspx#manipulating_unmanaged_resources"&gt;http://msdn.microsoft.com/en-us/library/ee787088.aspx#manipulating_unmanaged_resources&lt;/A&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It is managed in the sense that the interfaces are referenced from a managed PIA, but the underlying implementation is in COM. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In fact, you can't directly use COM objects, you use an RCW, which is managed and eligible for garbage collection, to interact and marshal data back and forth between your .NET code and COM code. The RCW will eventually be collected by the .NET GC and will release the COM object. But, the problem is that it is done at the whim of the GC and not immediately. In this case, the cursor needs to be released immediately because it has a table open, so we can't wait around on the GC to do its thing. Rather than waiting on the GC, you can tell the RCW to let go of the COM object by calling Marshal.ReleaseComObject on it. The RCW sticks around and waits to be collected by the GC, but it disconnects itself from the COM object it was helping you communicate with.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 24 Jan 2013 13:42:49 GMT</pubDate>
    <dc:creator>JasonPike</dc:creator>
    <dc:date>2013-01-24T13:42:49Z</dc:date>
    <item>
      <title>What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429054#M11604</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Coding gurus,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've got a question about best practise when using the ComReleaser object. In the first example below is some stub code showing how I would typically using ComReleaser when I'm processing within a single loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;Using comRel As New ComReleaser()&amp;nbsp;&amp;nbsp; &amp;nbsp; pFeatureCursor = pFeatureClass.Update(pQueryFilter, False) &amp;nbsp; comRel.ManageLifetime(pFeatureCursor) &amp;nbsp; pFeature = pFeatureCursor.NextFeature&amp;nbsp; &amp;nbsp; Do While Not pFeature Is Nothing &amp;nbsp;&amp;nbsp; ' Do something&amp;nbsp; pFeatureCursor.UpdateFeature(pFeature) &amp;nbsp;&amp;nbsp;&amp;nbsp; pFeature = pFeatureCursor.NextFeature &amp;nbsp; Loop &amp;nbsp; pFeatureCursor.Flush() End Using&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now suppose I want to loop through another cursor whilst looping through the first cursor, so nested. Is it appropriate to have a Using statement within a using statement (1) or do you simply add the inner cursor to the comReleaser object (2). I ask as I've not seen any examples and was wondering what was the best approach when nesting cursors?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-style:italic;"&gt;Example 1&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;Using comRel As New ComReleaser()&amp;nbsp;&amp;nbsp; &amp;nbsp; pFeatureCursor = pFeatureClass.Update(pQueryFilter, False) &amp;nbsp; comRel.ManageLifetime(pFeatureCursor) &amp;nbsp; pFeature = pFeatureCursor.NextFeature&amp;nbsp; &amp;nbsp; Do While Not pFeature Is Nothing &amp;nbsp;&amp;nbsp; ' Do something&amp;nbsp; &amp;nbsp; Using comRel2 as New ComReleaser() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pFeatureCursor2 = pFeatureClass2.Search(pQueryFilter2, False) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; comRel2.ManageLifetime(pFeatureCursor2) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Do something inner looping stuff&amp;nbsp; End Using &amp;nbsp;&amp;nbsp; pFeatureCursor.UpdateFeature(pFeature) &amp;nbsp;&amp;nbsp;&amp;nbsp; pFeature = pFeatureCursor.NextFeature &amp;nbsp; Loop &amp;nbsp; pFeatureCursor.Flush() End Using&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-style:italic;"&gt;Example 2&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;Using comRel As New ComReleaser()&amp;nbsp;&amp;nbsp; &amp;nbsp; pFeatureCursor = pFeatureClass.Update(pQueryFilter, False) &amp;nbsp; comRel.ManageLifetime(pFeatureCursor) &amp;nbsp; pFeature = pFeatureCursor.NextFeature&amp;nbsp; &amp;nbsp; Do While Not pFeature Is Nothing &amp;nbsp;&amp;nbsp; ' Do something &amp;nbsp;&amp;nbsp;&amp;nbsp; pFeatureCursor2 = pFeatureClass2.Search(pQueryFilter2, False) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; comRel.ManageLifetime(pFeatureCursor2) &amp;nbsp;&amp;nbsp;&amp;nbsp; ' Do something inner looping stuff &amp;nbsp;&amp;nbsp; End Using &amp;nbsp;&amp;nbsp; pFeatureCursor.UpdateFeature(pFeature) &amp;nbsp;&amp;nbsp;&amp;nbsp; pFeature = pFeatureCursor.NextFeature &amp;nbsp; Loop &amp;nbsp; pFeatureCursor.Flush() End Using&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In example 2&amp;nbsp; pFeatureCursor2 is added to comRel on every cycle of the outer cursor loop, is that good, bad or irrelevant?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Jan 2013 14:05:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429054#M11604</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2013-01-23T14:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429055#M11605</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;IMO, the best practice is not to use ESRI's ComReleaser class at all.&amp;nbsp; The best way to manage your COM objects is to do it yourself so that you know what's going on and have full control over it.&amp;nbsp; When looping through a cursor, you should also be releasing the row/feature objects within the loop before assigning the reference to the next item in the cursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Dim featureCursor As IFeatureCursor = featureClass.Search(Nothing, False)
Dim feature As IFeature = featureCursor.NextFeature
Do While feature IsNot Nothing
&amp;nbsp; ' do something with the feature

&amp;nbsp; Marshal.ReleaseComObject(feature)
&amp;nbsp; feature = featureCursor.NextFeature
Loop

Marshal.ReleaseComObject(featureCursor)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:18:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429055#M11605</guid>
      <dc:creator>NeilClemmons</dc:creator>
      <dc:date>2021-12-11T19:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429056#M11606</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I second what Neil said. I would add that you probably want to release in a finally-block so that it is guaranteed to happen.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I've copied my reply from a different forum thread (&lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/75186-Memory-leak-with-FeatureClass-object"&gt;http://forums.arcgis.com/threads/75186-Memory-leak-with-FeatureClass-object&lt;/A&gt;&lt;SPAN&gt;). It is probably way more than you were asking, but the short answer, in my opinion, is don't use ComReleaser:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;COM objects do not reside in managed memory; they reside on the unmanaged heap. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;COM objects are used in .NET through proxies called Runtime Callable Wrappers (RCWs) that reside on the managed heap.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;COM objects use reference counting to determine when they can be deallocated. They maintain a count of references to themselves and deallocate when that counter reaches 0. When a reference to a COM object is made in .NET, a RCW is created an the COM object is incremented once and only once for that instance.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;RCWs also maintain a reference count. It is separate from the underlying COM object's reference count; it tracks the number of times IUnknown is marshalled from the unmanaged heap to the managed heap. When the RCW's reference count reaches zero, it detachs itself from the COM object which causes the COM object's reference to decrement by 1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The most important thing to remember is that for an instance of an object (e.g. a layer object containing rivers, a feature class object containing cities, etc.) there is only one, shared RCW per application domain. If that RCW is detached from the COM object, then it is detached for every managed (read: .NET) object in that application domain, including .NET code that you didn't write and have no control over. Unless you've created your own application domain, every COM object you reference in your managed code will be loaded into the Shared application domain (1 of 3 application domains created by default for each .NET process) and shared with the rest of the managed code in the process. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Microsoft provides two methods for manually releasing COM objects for cases where deterministic memory management is required and the non-deterministic .NET garbage collector cannot be relied upon:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Marshal.ReleaseComObject - decrements the RCW's reference count by 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Marshal.FinalReleaseComObject - decrements the RCW's reference count until it reaches zero and detaches the underlying COM object&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code should only ever call ReleaseComObject as many times as it has incremented the reference count. If the RCW is released too many times, it can detach the underlying COM object before other objects that reference it are finished with it. When this happens, you get the very familiar: "COM object that has been separated from its underlying RCW cannot be used." &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;FinalReleaseCom object should only be used when the caller is absolutely sure no other objects are referencing the COM object--this includes managed code in the application domain that you may not have created. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As noted in other threads, ComReleaser essentially calls Marshal.FinalReleaseComObject for every object you've told it to manage. This means that you had better be sure that no other managed code in the application domain (your product, other 3rd parties, etc.) is holding a reference to the RCWs you've told it to manage. Since you can't know what other managed code might be running in the process, I think that it is unsafe to use ComReleaser--instead, COM interop should be understood and applied carefully using ReleaseComObject appropriately. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I understand that COM interop is an advanced programming topic and that the ComReleaser class was meant to lessen the pain for developers that didn't want to take the time learn it. However, this is when ComReleaser is most dangerous--when it is used by developers who don't understand its limitations and ramifications. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've had bugs filed against products I've developed only to find out that they only occurred when another product was installed alongside it and which eventually led to the discovery that the other product had mismanaged RCWs that our product happened to be using also. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, all that to say: ComReleaser is useful if you are absolutely sure you're passing it objects that are not referenced anywhere else in the application domain; otherwise, you risk the stability of the program when you use it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Resources:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://www.amazon.com/NET-COM-Comple.../dp/067232170X"&gt;http://www.amazon.com/NET-COM-Comple.../dp/067232170X&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://www.amazon.com/Advanced-NET-D.../dp/0321578899"&gt;http://www.amazon.com/Advanced-NET-D.../dp/0321578899&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx"&gt;http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://en.wikipedia.org/wiki/Reference_counting"&gt;http://en.wikipedia.org/wiki/Reference_counting&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/8bwh56xe.aspx"&gt;http://msdn.microsoft.com/en-us/library/8bwh56xe.aspx&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Jan 2013 16:03:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429056#M11606</guid>
      <dc:creator>JasonPike</dc:creator>
      <dc:date>2013-01-23T16:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429057#M11607</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Neil and Jason,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you for telling me why I should not be using comReleaser! ESRI use it in many of their code examples so this is why I have adopted it as I understood it guaranteed the release of the cursor but as Jason shows that is not always the case and using Marshal.ReleaseComObject is a more robust solution.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;BUT, I can't help noticing this does not answer my question...;)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you are using comReleaser what is the best practise when you have nested loops? Neil points out it is an ESRI class, may be one the the ESRI product developers can answer this question?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Duncan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 08:43:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429057#M11607</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2013-01-24T08:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429058#M11608</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Neil and Jason,&lt;BR /&gt;&lt;BR /&gt;Thank you for telling me why I should not be using comReleaser! ESRI use it in many of their code examples so this is why I have adopted it as I understood it guaranteed the release of the cursor but as Jason shows that is not always the case and using Marshal.ReleaseComObject is a more robust solution.&lt;BR /&gt;&lt;BR /&gt;BUT, I can't help noticing this does not answer my question...;)&lt;BR /&gt;&lt;BR /&gt;If you are using comReleaser what is the best practise when you have nested loops? Neil points out it is an ESRI class, may be one the the ESRI product developers can answer this question?&lt;BR /&gt;&lt;BR /&gt;Duncan&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Duncan,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your second example is the best answer--you only need one instance of the ComReleaser.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[CORRECTION: The second example is not the best answer--see Neil's response about making sure cursors are released inside the loop.]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, you may only end the using block once. I think the "End Using" in the inner loop that I highlighted in red was a cut and paste mistake.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;Using comRel As New ComReleaser()&amp;nbsp;&amp;nbsp; &amp;nbsp; pFeatureCursor = pFeatureClass.Update(pQueryFilter, False) &amp;nbsp; comRel.ManageLifetime(pFeatureCursor) &amp;nbsp; pFeature = pFeatureCursor.NextFeature&amp;nbsp; &amp;nbsp; Do While Not pFeature Is Nothing &amp;nbsp;&amp;nbsp; ' Do something &amp;nbsp;&amp;nbsp;&amp;nbsp; pFeatureCursor2 = pFeatureClass2.Search(pQueryFilter2, False) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; comRel.ManageLifetime(pFeatureCursor2) &amp;nbsp;&amp;nbsp;&amp;nbsp; ' Do something inner looping stuff &amp;nbsp;&amp;nbsp;&amp;nbsp; End Using &amp;nbsp;&amp;nbsp; pFeatureCursor.UpdateFeature(pFeature) &amp;nbsp;&amp;nbsp;&amp;nbsp; pFeature = pFeatureCursor.NextFeature &amp;nbsp; Loop &amp;nbsp; pFeatureCursor.Flush() End Using&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[EDIT: The paragraph below is still relevant, but Neil's response points out that releasing the COM object isn't the point--the point is having that COM object close the table it has opened, which only happens when it is released.]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The reason why you would want to add pFeatureCursor2 to the ComReleaser instance for each iteration of the inner loop is because every time you assign pFeatureCursor2 to the output of the Search() call pFeatureCursor2 is pointed to a different instance of IFeatureCursor. This means that under-the-hood a new instance of a COM object was created and returned to you and assigned to pFeatureCursor2. If you want that instance of the COM object to go away when the using block ends, you have to add it to the ComReleaser (or use the other approaches suggested by me and Neil.)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 12:12:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429058#M11608</guid>
      <dc:creator>JasonPike</dc:creator>
      <dc:date>2013-01-24T12:12:21Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429059#M11609</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The Using statement scopes the resource (the variable you declare) to the block of code between the Using and End Using statements.&amp;nbsp; The purpose of the Using statement is to limit the scope of a resource within a larger block of code and to guarantee that it is disposed as soon as the End Using statement is executed.&amp;nbsp; So, is it ok to nest Using statements?&amp;nbsp; Yes.&amp;nbsp; It's really no different than nesting loops or If/End If blocks.&amp;nbsp; In your case, you're using a cursor within a loop.&amp;nbsp; Each loop iteration opens a new cursor.&amp;nbsp; That cursor needs to be released with each loop iteration.&amp;nbsp; Each time a cursor is created, a table in the database is opened.&amp;nbsp; It will not be closed until the cursor is released, so if you don't release the cursor within each loop iteration it will keep opening tables and not closing them.&amp;nbsp; Eventually, you'll hit the max. number of open tables and an exception will be thrown.&amp;nbsp; So, that cursor needs to be managed by its own ComReleaser object declared within the loop.&amp;nbsp; If you use the ComRelease declared outside of the loop then the cursors created inside the loop will not be released until after the loop is exited.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 12:30:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429059#M11609</guid>
      <dc:creator>NeilClemmons</dc:creator>
      <dc:date>2013-01-24T12:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429060#M11610</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The Using statement scopes the resource (the variable you declare) to the block of code between the Using and End Using statements.&amp;nbsp; The purpose of the Using statement is to limit the scope of a resource within a larger block of code and to guarantee that it is disposed as soon as the End Using statement is executed.&amp;nbsp; So, is it ok to nest Using statements?&amp;nbsp; Yes.&amp;nbsp; It's really no different than nesting loops or If/End If blocks.&amp;nbsp; In your case, you're using a cursor within a loop.&amp;nbsp; Each loop iteration opens a new cursor.&amp;nbsp; That cursor needs to be released with each loop iteration.&amp;nbsp; Each time a cursor is created, a table in the database is opened.&amp;nbsp; It will not be closed until the cursor is released, so if you don't release the cursor within each loop iteration it will keep opening tables and not closing them.&amp;nbsp; Eventually, you'll hit the max. number of open tables and an exception will be thrown.&amp;nbsp; So, that cursor needs to be managed by its own ComReleaser object declared within the loop.&amp;nbsp; If you use the ComRelease declared outside of the loop then the cursors created inside the loop will not be released until after the loop is exited.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Agreed. I have updated my post.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 12:34:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429060#M11610</guid>
      <dc:creator>JasonPike</dc:creator>
      <dc:date>2013-01-24T12:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429061#M11611</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;IMO, the best practice is not to use ESRI's ComReleaser class at all.&amp;nbsp; The best way to manage your COM objects is to do it yourself so that you know what's going on and have full control over it.&amp;nbsp; When looping through a cursor, you should also be releasing the row/feature objects within the loop before assigning the reference to the next item in the cursor.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Dim featureCursor As IFeatureCursor = featureClass.Search(Nothing, False)
Dim feature As IFeature = featureCursor.NextFeature
Do While feature IsNot Nothing
&amp;nbsp; ' do something with the feature

&amp;nbsp; Marshal.ReleaseComObject(feature)
&amp;nbsp; feature = featureCursor.NextFeature
Loop

Marshal.ReleaseComObject(featureCursor)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is feature a managed or unmanaged object? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If it is a managed object, wouldn't the fact that the previous feature reference make it eligible for garbage collection in .NET?&amp;nbsp; Otherwise, I agree, you should release it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/ee787088.aspx#manipulating_unmanaged_resources" rel="nofollow noopener noreferrer" target="_blank"&gt;http://msdn.microsoft.com/en-us/library/ee787088.aspx#manipulating_unmanaged_resources&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:18:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429061#M11611</guid>
      <dc:creator>LeoDonahue</dc:creator>
      <dc:date>2021-12-11T19:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429062#M11612</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Is feature a managed or unmanaged object? &lt;BR /&gt;&lt;BR /&gt;If it is a managed object, wouldn't the fact that the previous feature reference make it eligible for garbage collection in .NET?&amp;nbsp; Otherwise, I agree, you should release it.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/ee787088.aspx#manipulating_unmanaged_resources"&gt;http://msdn.microsoft.com/en-us/library/ee787088.aspx#manipulating_unmanaged_resources&lt;/A&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It is managed in the sense that the interfaces are referenced from a managed PIA, but the underlying implementation is in COM. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In fact, you can't directly use COM objects, you use an RCW, which is managed and eligible for garbage collection, to interact and marshal data back and forth between your .NET code and COM code. The RCW will eventually be collected by the .NET GC and will release the COM object. But, the problem is that it is done at the whim of the GC and not immediately. In this case, the cursor needs to be released immediately because it has a table open, so we can't wait around on the GC to do its thing. Rather than waiting on the GC, you can tell the RCW to let go of the COM object by calling Marshal.ReleaseComObject on it. The RCW sticks around and waits to be collected by the GC, but it disconnects itself from the COM object it was helping you communicate with.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 13:42:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429062#M11612</guid>
      <dc:creator>JasonPike</dc:creator>
      <dc:date>2013-01-24T13:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429063#M11613</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;In fact, you can't directly use COM objects, you use an RCW, which is managed and eligible for garbage collection, to interact and marshal data back and forth between your .NET code and COM code. The RCW will eventually be collected by the .NET GC and will release the COM object. &lt;SPAN style="color:#0000ff;"&gt;But, the problem is that it is done at the whim of the GC and not immediately&lt;/SPAN&gt;. &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;Same issue in Java.&amp;nbsp; In Java, we can call garbage collection methods, but garbage collection will run when it wants according to whatever algorithm is uses.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&amp;nbsp; In this case, the cursor needs to be released immediately because it&amp;nbsp; has a table open, so we can't wait around on the GC to do its thing.&amp;nbsp; Rather than waiting on the GC, you can tell the RCW to let go of the COM&amp;nbsp; object by calling Marshal.ReleaseComObject on it. &lt;SPAN style="color:#0000ff;"&gt;The RCW sticks around&amp;nbsp; and waits to be collected by the GC, but it disconnects itself from the&amp;nbsp; COM object it was helping you communicate with&lt;/SPAN&gt;.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;So you're saying that your code will not wait for garbage collection, why is that?&amp;nbsp; Because of the nested cursors it is running slow, or ?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 14:01:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429063#M11613</guid>
      <dc:creator>LeoDonahue</dc:creator>
      <dc:date>2013-01-24T14:01:39Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429064#M11614</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Same issue in Java.&amp;nbsp; In Java, we can call garbage collection methods, but garbage collection will run when it wants according to whatever algorithm is uses.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;So you're saying that your code will not wait for garbage collection, why is that?&amp;nbsp; Because of the nested cursors it is running slow, or ?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Garbage collection will not run after each iteration of the loop, but each iteration of the loop opens a table. The tables need to be closed after each iteration because you can only have a limited number of tables open at one time. Garbage collection may not run until well after you leave the method that has the loop opening all the tables--it depends on things like memory pressure, generation size, etc.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 14:10:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429064#M11614</guid>
      <dc:creator>JasonPike</dc:creator>
      <dc:date>2013-01-24T14:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429065#M11615</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I wrote an Add-In that used a Windows form and many chained functions that create and release cursors using ReleaseComObject. Two of the functions use IDataStatistics, which takes a populated cursor as a parameter. When I was targeting ArcGIS 10.0 it all worked fine. Now with 10.1 I am getting a lot of errors about trying to access protected memory, which the MSDN help suggests is due to memory corruption, usually related to unmanaged code. The error help suggests that the memory corruption may be accumulating through many operations before triggering the error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The failure seems to always happen on the second or third pass that is triggered by user form updates and occurs within the subroutines that have the cursor that feeds to an IDataStatistics object. The error either appears when I call the UniqueValues method of the IDataStatistics object or when I do a Search on the feature cursor before it is passed to the IDataStatistics object (only on the second or third time through the code, never the first). Its behavior can be altered by the use or lack of use of the ReleaseComObject statements for the cursor or the IDataStatistics. I have tried all of the combinations I can think of and orders of the releaser. Below is a sample of how the code is written with the two lines where the protected memory error can occur shown in Red. Any suggestions on what objects I should use the ReleaseComObject on and the order or placement of that code or how to trap these errors and issolate the real cause?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Sub GetRoutesList()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Set Up Query Filter for the Full Date Range of the Collision Layer respecting PDO and DUI flags
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim pQF As IQueryFilter = New QueryFilterClass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pQF.WhereClause = "NOT " &amp;amp; fldCollisionDate &amp;amp; " IS NULL" &amp;amp; whereSeverity &amp;amp; whereDUI
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pQF.SubFields = fldCollisionDate &amp;amp; ", " &amp;amp; fldPrimaryRoad &amp;amp; ", " &amp;amp; fldRID &amp;amp; ", " &amp;amp; fldSeverity &amp;amp; ", party_sobriety_1, party_sobriety_2"

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Query Collisions Date Range and Street to fill Route Combobox List
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pQF.WhereClause = fldCollisionDate &amp;amp; " &amp;gt;= date '" &amp;amp; m_FromDate.ToString("yyyy-MM-dd HH:mm:ss") &amp;amp; "' AND " &amp;amp; fldCollisionDate &amp;amp; " &amp;lt;= date '" &amp;amp; m_ToDate.ToString("yyyy-MM-dd HH:mm:ss") &amp;amp; "' AND " &amp;amp; fldRID &amp;amp; " &amp;gt; ' ' AND Upper(" &amp;amp; fldPrimaryRoad &amp;amp; ") = '" &amp;amp; m_strPrimary &amp;amp; "'" &amp;amp; whereSeverity &amp;amp; whereDUI

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim dataStatisticsRoutes As IDataStatistics = New DataStatisticsClass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;Dim featCursorRouteList As IFeatureCursor = m_FLCollisions.Search(pQF, True)&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Use DataStatistics to get Unique Values
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataStatisticsRoutes.Field = fldRID
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataStatisticsRoutes.Cursor = featCursorRouteList
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;Dim pEnum As System.Collections.IEnumerator = dataStatisticsRoutes.UniqueValues()&lt;/STRONG&gt; 

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Fill the Combobox and suspend refresh until list is populated
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.BeginUpdate()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Clear the Routes Combobox
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.Items.Clear()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Reset the enumerator before trying to access its values
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pEnum.Reset()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Read each Unique value and populate the Route list.&amp;nbsp; Sorting is handled by a Combobox property.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For i As Int32 = 0 To dataStatisticsRoutes.UniqueValueCount - 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pEnum.MoveNext()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.Items.Add(UCase(pEnum.Current()))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next i

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Release the QueryFilter
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Not pQF Is Nothing Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Runtime.InteropServices.Marshal.ReleaseComObject(pQF)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Release the dataStatistics
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Not dataStatisticsRoutes Is Nothing Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Runtime.InteropServices.Marshal.ReleaseComObject(dataStatisticsRoutes)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Release the Cursor
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Not featCursorRouteList Is Nothing Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Runtime.InteropServices.Marshal.ReleaseComObject(featCursorRouteList)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Allow the Combobox to refresh
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.EndUpdate()

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If cboRoutes.Items.Contains(m_strRoute) Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Set Combobox Text to the current Map Route
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.Text = m_strRoute
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ElseIf cboRoutes.Items.Count &amp;gt; 0 Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Set Combobox Text to First Route in the List
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.Text = cboRoutes.Items(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_strRoute = cboRoutes.Text
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There are many other cursor calls that do not involve IDataStatistics and other Com objects from ArcGIS being created in the code. None of the cursors are embedded in each other and I have always used the ReleaseComObject call after I finish reading the cursor. These cursors have not generated errors so far, but I need to be sure they are not contributing to the problem. Any suggestions are appreciated.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:28:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429065#M11615</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-12T16:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429066#M11616</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; I wrote an Add-In that used a Windows form and many chained functions that create and release cursors using ReleaseComObject. Two of the functions use IDataStatistics, which takes a populated cursor as a parameter. When I was targeting ArcGIS 10.0 it all worked fine. Now with 10.1 I am getting a lot of errors about trying to access protected memory, which the MSDN help suggests is due to memory corruption, usually related to unmanaged code. The error help suggests that the memory corruption may be accumulating through many operations before triggering the error.&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt;The failure seems to always happen on the second or third pass that is triggered by user form updates and occurs within the subroutines that have the cursor that feeds to an IDataStatistics object. The error either appears when I call the UniqueValues method of the IDataStatistics object or when I do a Search on the feature cursor before it is passed to the IDataStatistics object (only on the second or third time through the code, never the first). Its behavior can be altered by the use or lack of use of the ReleaseComObject statements for the cursor or the IDataStatistics. I have tried all of the combinations I can think of and orders of the releaser. Below is a sample of how the code is written with the two lines where the protected memory error can occur shown in Red. Any suggestions on what objects I should use the ReleaseComObject on and the order or placement of that code or how to trap these errors and issolate the real cause?&amp;nbsp; &lt;BR /&gt; &lt;BR /&gt; &lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Sub GetRoutesList()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Set Up Query Filter for the Full Date Range of the Collision Layer respecting PDO and DUI flags
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim pQF As IQueryFilter = New QueryFilterClass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pQF.WhereClause = "NOT " &amp;amp; fldCollisionDate &amp;amp; " IS NULL" &amp;amp; whereSeverity &amp;amp; whereDUI
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pQF.SubFields = fldCollisionDate &amp;amp; ", " &amp;amp; fldPrimaryRoad &amp;amp; ", " &amp;amp; fldRID &amp;amp; ", " &amp;amp; fldSeverity &amp;amp; ", party_sobriety_1, party_sobriety_2"

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Query Collisions Date Range and Street to fill Route Combobox List
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pQF.WhereClause = fldCollisionDate &amp;amp; " &amp;gt;= date '" &amp;amp; m_FromDate.ToString("yyyy-MM-dd HH:mm:ss") &amp;amp; "' AND " &amp;amp; fldCollisionDate &amp;amp; " &amp;lt;= date '" &amp;amp; m_ToDate.ToString("yyyy-MM-dd HH:mm:ss") &amp;amp; "' AND " &amp;amp; fldRID &amp;amp; " &amp;gt; ' ' AND Upper(" &amp;amp; fldPrimaryRoad &amp;amp; ") = '" &amp;amp; m_strPrimary &amp;amp; "'" &amp;amp; whereSeverity &amp;amp; whereDUI

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim dataStatisticsRoutes As IDataStatistics = New DataStatisticsClass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG style="color: &amp;quot;#FF0000&amp;quot;;"&gt;Dim featCursorRouteList As IFeatureCursor = m_FLCollisions.Search(pQF, True)&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Use DataStatistics to get Unique Values
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataStatisticsRoutes.Field = fldRID
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataStatisticsRoutes.Cursor = featCursorRouteList
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG style="color: &amp;quot;#FF0000&amp;quot;;"&gt;Dim pEnum As System.Collections.IEnumerator = dataStatisticsRoutes.UniqueValues()&lt;/STRONG&gt; 

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Fill the Combobox and suspend refresh until list is populated
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.BeginUpdate()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Clear the Routes Combobox
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.Items.Clear()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Reset the enumerator before trying to access its values
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pEnum.Reset()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Read each Unique value and populate the Route list.&amp;nbsp; Sorting is handled by a Combobox property.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For i As Int32 = 0 To dataStatisticsRoutes.UniqueValueCount - 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pEnum.MoveNext()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.Items.Add(UCase(pEnum.Current()))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next i

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Release the QueryFilter
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Not pQF Is Nothing Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Runtime.InteropServices.Marshal.ReleaseComObject(pQF)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Release the dataStatistics
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Not dataStatisticsRoutes Is Nothing Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Runtime.InteropServices.Marshal.ReleaseComObject(dataStatisticsRoutes)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Release the Cursor
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Not featCursorRouteList Is Nothing Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Runtime.InteropServices.Marshal.ReleaseComObject(featCursorRouteList)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Allow the Combobox to refresh
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.EndUpdate()

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If cboRoutes.Items.Contains(m_strRoute) Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Set Combobox Text to the current Map Route
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.Text = m_strRoute
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ElseIf cboRoutes.Items.Count &amp;gt; 0 Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Set Combobox Text to First Route in the List
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cboRoutes.Text = cboRoutes.Items(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_strRoute = cboRoutes.Text
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If
&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub
&lt;/PRE&gt; &lt;BR /&gt; &lt;BR /&gt;There are many other cursor calls that do not involve IDataStatistics and other Com objects from ArcGIS being created in the code. None of the cursors are embedded in each other and I have always used the ReleaseComObject call after I finish reading the cursor. These cursors have not generated errors so far, but I need to be sure they are not contributing to the problem. Any suggestions are appreciated.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Richard,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It sounds like you are having problems similar to the ones described in this thread:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/75186-Memory-leak-with-FeatureClass-object" rel="nofollow noopener noreferrer" target="_blank"&gt;http://forums.arcgis.com/threads/75186-Memory-leak-with-FeatureClass-object&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've described several debugging techniques in the thread that you might find helpful. If anyone has tried using the WinDbg or Application Verifier, they haven't posted their results. I would start with WinDbg to see what calls are in the stack with the Search() and UniqueValues() call when they crash. If it happens to be the same underlying calls, it could really shed some light on what is going wrong. If you decide to try WinDbg, don't forget to set the _NT_SYMBOL_PATH environment variable so that the ArcObject calls are fully resolved.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;At least one of the developers on the other thread has filed a support ticket with ESRI, so you might want to monitor that thread as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you can narrow the problem down to a small stand-alone app, zip up the entire solution, and attach it to one of your posts, I'll be happy to spend some time debugging it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jason&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:18:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429066#M11616</guid>
      <dc:creator>JasonPike</dc:creator>
      <dc:date>2021-12-11T19:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429067#M11617</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Richard,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Did you see this &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/70997-ICursor-issue-in-10.1-SP-1"&gt;thread&lt;/A&gt;&lt;SPAN&gt; that discovered the bug NIM087476&amp;nbsp; �??Memory leakage ESRI.ArcGIS.Geodatabase.IDataStatistics using UniqueValues property&amp;nbsp; with ArcGIS 10.1 with SP1�?�&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 12:13:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429067#M11617</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2013-01-25T12:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429068#M11618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Richard,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was having ArcMap crash spectacularly and it was down to the IDatastatistics object as indicated by the thread that Ken has linked to. It took a few days of headbanging a brick wall and all manner of cursing before I discovered that thread so I re-engineered the code to use a cursor to obtain unique values. It is clearly a bug introduced into 10.1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Everyone else thanks for your advice on best practise when using comReleaser (which seems to be don't use it! &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt; )&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Duncan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 15:31:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429068#M11618</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2013-01-25T15:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best practise when using ComReleaser?</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429069#M11619</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Richard,&lt;BR /&gt;&lt;BR /&gt;Did you see this &lt;A href="http://forums.arcgis.com/threads/70997-ICursor-issue-in-10.1-SP-1"&gt;thread&lt;/A&gt; that discovered the bug NIM087476&amp;nbsp; �??Memory leakage ESRI.ArcGIS.Geodatabase.IDataStatistics using UniqueValues property&amp;nbsp; with ArcGIS 10.1 with SP1�?�&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for pointing that out.&amp;nbsp; I had not seeen this thread, but this explains why it worked at 10.0 and not at 10.1 SP1 (I skipped 10.1 original).&amp;nbsp; I will reengineer the code to use a cursor to get unique values.&amp;nbsp; Can anyone pioint me to any threads that show the most elegant techniques for doing that?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jan 2013 13:33:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/what-is-the-best-practise-when-using-comreleaser/m-p/429069#M11619</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2013-01-28T13:33:53Z</dc:date>
    </item>
  </channel>
</rss>

