c# - I want my memory back! How can I truly dispose a control? -


i have application making creates large number of windows controls (buttons , labels etc). being made dynamically through functions. problem i'm having is, when remove controls , dispose them, not removed memory.

void loadaloadofstuff() {     while(tabcontroltoclear.controls.count > 0)         tabcontroltoclear.controls[0].dispose();     //i put in:     gc.collect();     gc.waitforpendingfinalizers();     foreach(string pagename in globallist)         tabcontroltoclear.controls.add(makeatab(pagename)); }  tabpage makeatab(string tabtext) {     tabpage newt = new makeatab();     newt.text = tabtext;     //fill page controls methods     return newt; } 

now reason, ain't giving me memory back, when process runs 5 times, end out of memory violation. new object , control disposal looking through vast net still hasn't given me indications, if of have idea, i'd grateful hear it.

update: i've been watching user objects creation , destruction (taskmanager) , noticed create tab page, add click handler, add panel, add 2 buttons both click handlers, tooltips , backimages (i think problem is). app says creates 8 new items, when run through dispose, remove 4 memory. i've been trying remove event handlers, seems make no difference.

resolved!!! adding new items panel, passing them tooltip (stupid, i'm learning). else has same problem, (thanks comments , directions people below. discovered in order make control dispose (as realise incorrectly put it) is:

1: if have tool tip, make sure it's accessable! not did! e.g:

this wrong!

tabpage makeatab(string tabtext) {     tabpage newt = new makeatab();     tooltip mytip = new tooltip();     newt.text = tabtext;     //fill page controls methods     mytip.settooltip(newt, "something say");     return newt; } 

if this, lose pointer tooltip, , tooltip not child of object it's connected (rather, tooltip makes strong reference control), if destroy control, tooltip can't access keeps object alive.

2: before anything, call tooltip.removeall(). removes it's ties controls. note, if using tip other controls, lost tool tip.

3: remove internal controls base control.controlcollection (if use non managed memory, guess. i'm doing cause it's making app work so...)

4: remove custom event handlers.

5: finally, dispose object. made quick recursing function quite well.

    private void recursivedispose(control todispose)     {         while (todispose.controls.count > 0)             recursivedispose(todispose.controls[0]);          if (todispose.backgroundimage != null)             backgroundimage = null;          if (todispose.gettype() == typeof(button))             todispose.click -= [your event];         else if (todispose.gettype() == typeof(tabpage))             todispose.doubleclick -= [your event];         else if (todispose.gettype() == typeof(label))             todispose.mousemove -= [your event];          todispose.dispose();     } 

this extremely crude , there's better way of doing it, unless can come it, have do. everyone. might have saved entire project.

you need clear out reference.

while(tabcontroltoclear.controls.count > 0) {      var tabpage = tabcontroltoclear.controls[0];     tabcontroltoclear.controls.removeat(0);     tabpage.dispose();       // clear out events.      foreach (eventhandler subscriber in tabpage.click.getinvocationlist())     {         tabpage.click -= subscriber;     } } 

Comments

Popular posts from this blog

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -

openssl - Load PKCS#8 binary key into Ruby -