0

I have many user control when i click on my menus button it shows that user control i want but i was looking on GDI objects on the task manager i found that when i go to another user control the GDI not released and not dispose it is my code for adding user control to panel and dispose any user control on that panel before adding new user control:

void showControl(Control control)  
        {  
            for (int ix = tlp4.Controls.Count - 1; ix >= 0; --ix)  
            {  
                var ctl = tlp4.Controls[ix];  
  
                ctl.Dispose();  
            }  
  
            tlp4.BringToFront();  
              
            control.Dock = DockStyle.Fill;  
            tlp4.Controls.Add(control);  
            control.Focus();  
            tableLayoutPanel2.Visible = false;  
              
            tlp4.Visible = true;  
        }  
noobpro
  • 1
  • 2
  • You don't have to explicitly Dispose() of them, the Garbage Collector will do that eventually. Just Clear() the Panel and add your new one. – Idle_Mind Jun 23 '20 at 20:42
  • @Idle_Mind and what about the GDI OBJECT why it is increasing and not decreasing? – noobpro Jun 23 '20 at 21:27
  • Because .Net is notorious for being a memory hog. It tends to hold on to memory, "just in case you need it again soon". Trust me, it'll release the memory when your system actually needs it for other programs. Unless you are doing something weird in those controls, I wouldn't worry about it. Are you disposing of all pens/brushes/fonts/etc if you create them? Are you creating Graphics resources? – Idle_Mind Jun 23 '20 at 21:34
  • @Idle_Mind No my user controls includes datagridview. but after a while when use my application it stop and give me out of memory and at that moment i go and see GDI OBJECT in task manager and it is over 10000 because of that my app is stoped working – noobpro Jun 23 '20 at 21:39
  • How many of those UserControls are you making? How often does that panel get reset and reloaded? – Idle_Mind Jun 23 '20 at 21:45
  • @Idle_Mind it is 9 user control – noobpro Jun 23 '20 at 21:52
  • @Idle_Mind i don't understand the second question – noobpro Jun 23 '20 at 21:53
  • I mean, are you clearing and reloading that panel in a Timer Tick() event maybe, or in response to an event? Do the UserControls themselves have something in them that is re-occurring quite often? – Idle_Mind Jun 23 '20 at 21:57
  • @Idle_Mind no i am not clearing panel. and the user control does not have anything that repeating – noobpro Jun 23 '20 at 22:00
  • Something else is going on in your app then. "normal" apps don't consume that many GDI objects and crash. We'd need to know more about your app to figure it out. – Idle_Mind Jun 23 '20 at 22:04
  • @Idle_Mind what i do? – noobpro Jun 23 '20 at 22:06
  • Are you creating/displaying controls from within a Paint() event maybe? You could try finding a **MEMORY PROFILER** to see if something jumps out at you. I've seen other posts where a STATIC DataTable in a Form/UserControl can keep them from being disposed. – Idle_Mind Jun 23 '20 at 22:15
  • @Idle_Mind no i am not using any paint event – noobpro Jun 23 '20 at 22:24
  • @Idle_Mind i don't have static DataTable but i have some other static object – noobpro Jun 23 '20 at 22:28
  • @Idle_Mind and some static variables – noobpro Jun 23 '20 at 22:29
  • Static variables might not be the problem...depends how they are used. Just somewhere to look and start searching. – Idle_Mind Jun 23 '20 at 22:34
  • @Idle_Mind i am searching more than one week and too know i don't find any solution – noobpro Jun 23 '20 at 22:48
  • I'd update your description with more details about what your app actually does. What external resources does it access? Etc. It might help someone else find the problem. – Idle_Mind Jun 23 '20 at 22:56
  • @Idle_Mind it is connecting to MySQL database my datagridview – noobpro Jun 23 '20 at 23:04
  • @Idle_Mind but i think when dispose user control it is not dispose controls on that user control i think it is a problem and i don't know how to solve it – noobpro Jun 23 '20 at 23:06
  • If you think it's an issue with databinding and the grid, set the datasource to null (or otherwise "unbind" it) before you dispose of it? – Idle_Mind Jun 23 '20 at 23:07
  • @Idle_Mind i don't think it is the problem – noobpro Jun 23 '20 at 23:17

0 Answers0