-2

Relatively simple problem.

I have a panel with some textboxes on it, all dynamically created. The user fills in some of the textboxes and then proceeds to close the panel.

Now in the code I use the line;

Me.Pnl_Main.Controls.Clear()

and this works fine, the panel contents are "removed".

The problem is, is that when the textboxes are recreated for the same purpose, they still contain the values they had previously.

And unfortunately for me, most of the UI is created like this, which inevitably leads to a memory leak.

So my question is, is there a proper way to remove a control completly from memory? Or do I need to run a routine to set all text values to Nothing?

Thanks in advance.

Bob.Tri
  • 55
  • 6
  • I think the problem is that references to the textboxes you created still exists somewhere after you executing `.Clear()`. I don't think you have memory leak problem, because you always using same instances of textboxes(that why you see previous text). Of course of textboxes are referenced outside of the form, than leak is possible. Can you show a code how you create textboxes and add them to the Panel – Fabio Dec 23 '16 at 12:15
  • After `Me.Pnl_Main.Controls.Clear()` you can set the textbox variable to nothing. – Aaditya Dengle Dec 23 '16 at 12:19
  • The textboxes are actually part of a user control I made (this way I save time when dynamically creating large quantities of controls). – Bob.Tri Dec 23 '16 at 12:21
  • Public PG1 As New ConditionReportPage1 Me.pnl_main.controls.add(PG1) – Bob.Tri Dec 23 '16 at 12:22
  • If textboxes are part of UserControl then you don't need to worry about memory leak. When UserControl will be disposed textboxes will too. – Fabio Dec 23 '16 at 12:26
  • Is that true? I use the diagnostic tools provided with VS, and the memory taken in RAM never seems to go down when controls are "removed". Btw, setting them to nothing solves the issue for remembering previous values – Bob.Tri Dec 23 '16 at 12:33
  • Disposing an object has nothing specifically to do with memory. A disposed object can be removed from memory sooner but .NET will reclaim memory when it's good and ready. Allocating new memory is far cheaper than reallocating existing memory so that's what gets done most of the time. – jmcilhinney Dec 23 '16 at 12:41
  • Ah ok, I suppose that has something to do with the garbage collector. Thank you all for your help. – Bob.Tri Dec 23 '16 at 13:11

2 Answers2

-1

Disposing an object has nothing specifically to do with memory. A disposed object can be removed from memory sooner but .NET will reclaim memory when it's good and ready. Allocating new memory is far cheaper than reallocating existing memory so that's what gets done most of the time.

Bob.Tri
  • 55
  • 6
  • This is sort of true, sort of not. Calling Dispose has nothing at all do with the garbage collector; it releases *unmanaged* memory, while the garbage collector deals with managed memory. So calling Dispose *is* critical to prevent a memory leak. If a type implements IDisposable, you *must* call its Dispose method, or you'll leak. The Control class is even more special (see duplicate for more details). You absolutely *must* call the Dispose method for each control that you dynamically remove from its parent. The way to make the answer correct would be to replace "disposed" with "unreferenced". – Cody Gray - on strike Dec 23 '16 at 14:36
-1

So long as some part of your code holds a reference to your controls they will not be cleared from memory. You must remove all references to these controls e.g.

 Me.Pnl_Main.Controls = Nothing

Provided these controls are not referenced anywhere else they can be removed from memory but you don't actually control when they are finally removed from memory. This is done by the garbage collector that will run automatically generally when memory starts to get full.

mark_h
  • 5,233
  • 4
  • 36
  • 52
  • This literally does nothing. There is no point in setting things to `Nothing` in VB.NET. The code in the original question was correct, Bob should be calling `Clear`. The part he's missing is that he needs to loop through each control in the collection first and call its `Dispose` member function. Once that's done, he can call `Clear` to empty the collection. – Cody Gray - on strike Dec 23 '16 at 14:28
  • See this question, and the ones it links: http://stackoverflow.com/questions/1969705/clear-controls-does-not-dispose-them-what-is-the-risk – Cody Gray - on strike Dec 23 '16 at 14:32