<?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: .NET memory error on closing ArcMap with open control form in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644325#M17328</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Okay, I think I've solved this.&amp;nbsp; Originally my forms were all modal and I converted them to modeless using several examples I found on the ArcGIS forums and elsewhere on the Internets.&amp;nbsp; What I think happened is I ended up with a combination of methods that were somehow incompatible with each other.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I did since my previous posts was I created a brand new "Hello World" form, defined it by itself (i.e. not inside another class) and simply followed the example given here: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.arcgis.com/threads/53681-Opening-Visual-Studio-in-ArcGIS" rel="nofollow" target="_blank"&gt;http://forums.arcgis.com/threads/53681-Opening-Visual-Studio-in-ArcGIS&lt;/A&gt;&lt;SPAN&gt; to set up a modeless form.&amp;nbsp; My exact code is as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;'In a separate module containing project scope variables and constants...&amp;nbsp; Friend frm1 As Form1&amp;nbsp; ------------------------------------------------------------------- 'In the BaseCommand class "button"...&amp;nbsp; Public Overrides Sub OnClick() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If IsNothing(frm1) OrElse frm1.IsDisposed Then &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frm1 = New Form1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frm1.Show(New ModelessDialog(m_application.hWnd)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Else &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frm1.Show() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If End Sub&amp;nbsp; ------------------------------------------------------------------- 'Then the form code itself...&amp;nbsp; Public Class Form1&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If e.CloseReason = Windows.Forms.CloseReason.UserClosing Then &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Cancel = True &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Me.Hide() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If &amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub End Class&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This successfully created a singleton modeless form and does not cause those .NET errors after ArcMap closes.&amp;nbsp; Now I just need to apply this to my actual forms.&amp;nbsp; Wish me luck......&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 27 Mar 2012 18:06:49 GMT</pubDate>
    <dc:creator>KeithOlson</dc:creator>
    <dc:date>2012-03-27T18:06:49Z</dc:date>
    <item>
      <title>.NET memory error on closing ArcMap with open control form</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644323#M17326</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am developing a custom set of tools for ArcMap implementing BaseToolBar and BaseCommand in VB.NET.&amp;nbsp; For non-admin users, whenever the ArcMap application is closed with one of the tool's modeless forms still open, the following .NET error message appears:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Research into this issue has suggested that my COM objects are not being disposed of properly before the application shuts down.&amp;nbsp; Following the advise here: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://edndoc.esri.com/arcobjects/9.2/NET/fe9f7423-2100-4c70-8bd6-f4f16d5ce8c0.htm#AOShutdown" rel="nofollow" target="_blank"&gt;http://edndoc.esri.com/arcobjects/9.2/NET/fe9f7423-2100-4c70-8bd6-f4f16d5ce8c0.htm#AOShutdown&lt;/A&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I added an AOUninitialize.Shutdown command at the form's Disposed event, but this did not make any difference.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One of my forms is a simple "About" dialog that only shows version info and does nothing else.&amp;nbsp; This form references no ArcObjects other than the fact that you open it from a BaseCommand button.&amp;nbsp; So, there are no specific COM objects that need to be released or anything.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One thing that will prevent this error from occurring is to open the form as modal.&amp;nbsp; But I've seen and used other custom tools with modeless dialogs that do not have this issue.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any advice/suggestions would be greatly appreciated.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Mar 2012 16:11:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644323#M17326</guid>
      <dc:creator>KeithOlson</dc:creator>
      <dc:date>2012-03-26T16:11:37Z</dc:date>
    </item>
    <item>
      <title>Re: .NET memory error on closing ArcMap with open control form</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644324#M17327</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;A little more background info on this...&amp;nbsp; All my forms are set to have the ArcMap application as the parent window and are treated as singleton forms so I don't have more than on instance open at a time.&amp;nbsp; The form classes are all defined in a separate module in a class called clsGlobals.&amp;nbsp; Here's an example of the OnClick method of one of my tools (the "Apply Template" tool):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'...in the COM class that inherits BaseCommand (i.e., a button on an ArcMap toolbar).

Public Overrides Sub OnClick()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Try
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'Set pMxDoc and other variables used by multiple tools
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetMxDocumentFromArcMap(m_application)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'Make sure there are maps and layers in the document
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Not CheckFrames() Then Exit Sub

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'create form and open it.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If IsNothing(clsGlobals.frmTemplate) OrElse clsGlobals.frmTemplate.IsDisposed Then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clsGlobals.frmTemplate = New TemplateForm(pMxDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetWindowLong(clsGlobals.frmTemplate.Handle.ToInt32, GWL_HWNDPARENT, pApp.hWnd)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clsGlobals.frmTemplate.Show()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Else
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clsGlobals.frmTemplate.WindowState = FormWindowState.Normal
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clsGlobals.frmTemplate.BringToFront()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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; Catch ex As Exception
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExHandling(ex)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Try
End Sub&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also added this to the code on the actual form itself, which does not seem to make a difference:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Private Sub TemplateForm_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Disposed
&amp;nbsp;&amp;nbsp;&amp;nbsp; ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown()
End Sub&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway, if there is a better way to implement modeless forms or if there's any important piece I'm leaving out, I'd be very grateful for your advice.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 03:22:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644324#M17327</guid>
      <dc:creator>KeithOlson</dc:creator>
      <dc:date>2021-12-12T03:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: .NET memory error on closing ArcMap with open control form</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644325#M17328</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Okay, I think I've solved this.&amp;nbsp; Originally my forms were all modal and I converted them to modeless using several examples I found on the ArcGIS forums and elsewhere on the Internets.&amp;nbsp; What I think happened is I ended up with a combination of methods that were somehow incompatible with each other.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I did since my previous posts was I created a brand new "Hello World" form, defined it by itself (i.e. not inside another class) and simply followed the example given here: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.arcgis.com/threads/53681-Opening-Visual-Studio-in-ArcGIS" rel="nofollow" target="_blank"&gt;http://forums.arcgis.com/threads/53681-Opening-Visual-Studio-in-ArcGIS&lt;/A&gt;&lt;SPAN&gt; to set up a modeless form.&amp;nbsp; My exact code is as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;'In a separate module containing project scope variables and constants...&amp;nbsp; Friend frm1 As Form1&amp;nbsp; ------------------------------------------------------------------- 'In the BaseCommand class "button"...&amp;nbsp; Public Overrides Sub OnClick() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If IsNothing(frm1) OrElse frm1.IsDisposed Then &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frm1 = New Form1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frm1.Show(New ModelessDialog(m_application.hWnd)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Else &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frm1.Show() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If End Sub&amp;nbsp; ------------------------------------------------------------------- 'Then the form code itself...&amp;nbsp; Public Class Form1&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If e.CloseReason = Windows.Forms.CloseReason.UserClosing Then &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Cancel = True &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Me.Hide() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If &amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub End Class&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This successfully created a singleton modeless form and does not cause those .NET errors after ArcMap closes.&amp;nbsp; Now I just need to apply this to my actual forms.&amp;nbsp; Wish me luck......&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Mar 2012 18:06:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644325#M17328</guid>
      <dc:creator>KeithOlson</dc:creator>
      <dc:date>2012-03-27T18:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: .NET memory error on closing ArcMap with open control form</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644326#M17329</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks kdolson so much!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Nov 2012 01:29:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/net-memory-error-on-closing-arcmap-with-open/m-p/644326#M17329</guid>
      <dc:creator>MarkNguyen</dc:creator>
      <dc:date>2012-11-06T01:29:25Z</dc:date>
    </item>
  </channel>
</rss>

