<p>In one of my VB6 forms, I create several other Form objects and store them in member variables.</p>

<pre><code>Private m_frm1 as MyForm
Private m_frm2 as MyForm

// Later...
Set m_frm1 = New MyForm
Set m_frm2 = New MyForm
</code></pre>

<p>I notice that I'm leaking memory whenever this (parent) form is created and destroyed.  Is it necessary for me to assign these member variables to <code>Nothing</code> in <code>Form_Unload()</code>?</p>

<p>In general, when is that required?</p>

<p>SOLVED: This particular memory leak was fixed when I did an <code>Unload</code> on the forms in question, not when I set the form to <code>Nothing</code>.  I managed to remove a few other memory leaks by explicitly setting some instances of Class Modules to <code>Nothing</code>, as well.</p>

[{'answer_id': 30378, 'author': 'Allain Lalonde', 'author_id': 2443, 'author_profile': 'https://Stackoverflow.com/users/2443', 'pm_score': 2, 'selected': False, 'text': "<p>Strictly speaking never, but it gives the garbage collector a strong hint to clean things up.</p>\n\n<p>As a rule: <strong>do it every time you're done with an object that you've created</strong>.</p>\n"}, {'answer_id': 30382, 'author': 'Martin Marconcini', 'author_id': 2684, 'author_profile': 'https://Stackoverflow.com/users/2684', 'pm_score': 2, 'selected': False, 'text': '<p>Setting a VB6 reference to Nothing, decreases the refecences count that VB has for that object. If and only if the count is zero, then the object will be destroyed.</p>\n\n<p>Don\'t think that just because you set to Nothing it will be "garbage collected" like in .NET</p>\n\n<p>VB6 uses a reference counter. </p>\n\n<p>You are encouraged to set to "Nothing" instanciated objects that make referece to C/C++ code and stuff like that. It\'s been a long time since I touched VB6, but I remember setting files and resources to nothing. </p>\n\n<p>In either case it won\'t hurt (if it was Nothing already), but that doesn\'t mean that the object will be destroyed.</p>\n\n<p>VB6 had a "With/End With" statement that worked "like" the Using() statement in C#.NET. And of course, the less global things you have, the better for you.</p>\n\n<p>Remember that, in either case, sometimes creating a large object is more expensive than keeping a reference alive and reusing it. </p>\n'}, {'answer_id': 30383, 'author': 'Josh Miller', 'author_id': 2818, 'author_profile': 'https://Stackoverflow.com/users/2818', 'pm_score': 2, 'selected': False, 'text': '<p>I had a problem similar to this a while back.  I seem to think it would also prevent the app from closing, but it may be applicable here.</p>\n\n<p>I pulled up the old code and it looks something like:</p>\n\n<pre><code>Dim y As Long\nFor y = 0 To Forms.Count -1\n    Unload Forms(x)\nNext\n</code></pre>\n\n<p>It may be safer to Unload the m_frm1. and not just set it to nothing.</p>\n'}, {'answer_id': 30398, 'author': 'BZ.', 'author_id': 2349, 'author_profile': 'https://Stackoverflow.com/users/2349', 'pm_score': 2, 'selected': False, 'text': '<p>@Martin</p>\n\n<blockquote>\n  <p>VB6 had a "With/End With" statement that worked "like" the Using() statement in C#.NET. And of course, the less global things you have, the better for you.</p>\n</blockquote>\n\n<p>With/End With does not working like the Using statement, it doesn\'t "Dispose" at the end of the statement. </p>\n\n<p>With/End With works in VB 6 just like it does in VB.Net, it is basically a way to shortcut object properties/methods call. e.g.</p>\n\n<pre><code>With aCustomer\n  .FirstName = "John"\n  .LastName = "Smith"\nEnd With\n</code></pre>\n'}, {'answer_id': 30445, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<p>Actually, VB6 implements <a href="http://en.wikipedia.org/wiki/RAII" rel="noreferrer">RAII</a> just like C++ meaning that locally declared references automatically get set to <code>Nothing</code> at the end of a block. Similarly, it <em>should</em> automatically reset member class variables after executing <code>Class_Terminate</code>. However, there have been several reports that this is not done reliably. I don\'t remember any rigorous test but it has always been best practice to reset member variables manually.</p>\n'}, {'answer_id': 30679, 'author': 'Josh Miller', 'author_id': 2818, 'author_profile': 'https://Stackoverflow.com/users/2818', 'pm_score': 4, 'selected': True, 'text': "<p>@Matt Dillard - Did setting these to nothing fix your memory leak?</p>\n\n<p>VB6 doesn't have a formal garbage collector, more along the lines of what @Konrad Rudolph said.</p>\n\n<p>Actually calling unload on your forms seems to me to be the best way to ensure that the main form is cleaned up and that each subform cleans up their actions.</p>\n\n<p>I tested this with a blank project and two blank forms.</p>\n\n<pre><code>Private Sub Form_Load()\n  Dim frm As Form2\n  Set frm = New Form2\n  frm.Show\n  Set frm = Nothing\nEnd Sub\n</code></pre>\n\n<p>After running both forms are left visible.  setting frm to nothing did well... nothing.</p>\n\n<p>After settign frm to nothing, the only handle open to this form is via the reference.</p>\n\n<pre><code>Unload Forms(1)\n</code></pre>\n\n<p>Am I seeing the problem correctly?</p>\n\n<ul>\n<li>Josh</li>\n</ul>\n"}, {'answer_id': 148326, 'author': 'Guillermo Phillips', 'author_id': 441661, 'author_profile': 'https://Stackoverflow.com/users/441661', 'pm_score': 3, 'selected': False, 'text': "<p>Objects in VB have reference counting.  This means that an object keeps a count of how many other object variables hold a reference to it. When there are no references to the object, the object is garbage collected (eventually).  This process is part of the COM specification. </p>\n\n<p>Usually, when a locally instantiated object goes out of scope (i.e. exits the sub), its reference count goes down by one, in other words the variable referencing the object is destroyed.  So in most instances you won't need to explicitly set an object equal to Nothing on exiting a Sub.</p>\n\n<p>In all other instances you must explicitly set an object variable to Nothing, in order to decrease its reference count (by one).  Setting an object variable to Nothing, will not necessarily destroy the object, you must set ALL references to Nothing.  This problem can become particularly acute with recursive data structures.</p>\n\n<p>Another gotcha, is when using the New keyword in an object variable declaration.  An object is only created on first use, not at the point where the New keyword is used.  Using the New keyword in the declaration will re-create the object on first use every time its reference count goes to zero.  So setting an object to Nothing may destroy it, but the object will automatically be recreated if referenced again.  Ideally you should not declare using the New keyword, but by using the New operator which doesn't have this resurrection behaviour.</p>\n"}, {'answer_id': 1163450, 'author': 'Joe', 'author_id': 13087, 'author_profile': 'https://Stackoverflow.com/users/13087', 'pm_score': 2, 'selected': False, 'text': '<p>One important point that hasn\'t yet been mentioned here is that setting an object reference to Nothing will cause the object\'s destructor to run (Class_Terminate if the class was written in VB) if there are no other references to the object (reference count is zero).</p>\n\n<p>In some cases, especially when using a RAII pattern, the termination code can execute code that can raise an error.  I believe this is the case with some of the ADODB classes.  Another example is a class that encapsulates file i/o - the code in Class_Terminate might attempt to flush and close the file if it\'s still open, which can raise an error.</p>\n\n<p>So it\'s important to be aware that setting an object reference to Nothing can raise an error, and deal with it accordingly (exactly how will depend on your application - for example you might ignore such errors by inserting "On Error Resume Next" just before "Set ... = Nothing").</p>\n'}]
['https://Stackoverflow.com/questions/30354', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/863/']