Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

WPF Memory Leak

Posted on 2009-03-31
13
Medium Priority
?
1,124 Views
Last Modified: 2013-11-12
Hello every one,
I have encountered a strange situation in my application developed using WPF.
The memory usage in the task manager is gradually increasing and the system is getting creashed.
The memory is gradually incresing as i move from form1 to form 2 using the code on click event
form2 obj=new form2();
obj.someProperty=this.SomeProperty;
this.close();
obj.showDialog();
I'm explicitly killing the objects in form close as its not necessary as we are closing the form.
0
Comment
Question by:kmsmabu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24034572
r u calling form2.close()?
0
 

Author Comment

by:kmsmabu
ID: 24034790
Hi unmeshdave:
I need to open form2 from form1 and close the form1 object.
If i use form1.close the form1 is getting closed but the memory is not released
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24036256
the very simple reason for this is, you are trying to use the thread/memory which is already made free on onclose event. I mean, when you can use form1.hide() instead of close and then when u close form2, you can close form1 as well.
 

  private void button1_Click(object sender, EventArgs e)
        {
            Form2 obj = new Form2(this);
            this.Hide();
            obj.Show();
        }
===========Form2 class=====================
    public partial class Form2 : Form
    {
        Form1 frmPrev;
        public Form2(Form1 frm1)
        {
            InitializeComponent();
            frmPrev = frm1;
        }
 
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmPrev.Close();
        }
 
    }

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:kmsmabu
ID: 24041554
Hi,
The snippet that u hvae placed is abt windows forms.
form1 and form2 are wpf window objects
Even i chkd with ur implimentation My concern is the memory is gradually incresing.I will be opening the forms like
Win1-->Win2--Win3--Win4.......Win20
I close the Win1 and open Win2 though the memory usage in task manager is gradually increasing.
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24041651
the reason for memory leak I already told you. you are createing form2 inside handler of form1 and then you release form1 object without releasing form2 becuase you want your form2 to display. So this will never show your form2 and crash your application.
I havent tried this with WPF. but the solution I gave to you will work with WPF. Its about logic not with technology.
0
 

Author Comment

by:kmsmabu
ID: 24041831
Ya Wat ever u said is reasonalbe.
But the code above
Window1 objNext=new Window1();
objNext.Property=value;
this.close();
objNext.Showdialog();

The Next window is getting showed eventhough i am closing the parent before showdialog()
Even i was amazed by this but its  working in WPF.
At the same time the memory goes on increasing.could you plz help on this
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24041914
well I tried with WPF. I am not able to see memory leak with the code u mentioned. I guees u put some more code to understand what exactly you are doing?
show what you are doing in closed event as well.
0
 

Author Comment

by:kmsmabu
ID: 24041974
In closed event of window i will be assigning the objects to null
Try to have some controls in the both the forms
and navigate from window1 to window2  closing the window1
and from window2 to back to window1 closing the window2
The memory usage 1st when u open Window1 will be lessthan that of when u come from window2 to window1
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24042066
I can do it myself but how you are implementing that is important. That is the reason I  am asking for your code. what I notice is not inside application, but even after you close application, CLR takes some time to release memory.
0
 

Author Comment

by:kmsmabu
ID: 24056844
In main i have an static variable of type window Default.xaml and instance is created in the main and loaded from main as showdialog
I have a default form where i will be loading the other windows onload event i will be calling login page
----Default.xaml------

load--->ShowLogin(null);

public void ShowLogin(Window objParentWin)
        {
            if (objParentWin != null)
            {
                objParentWin.Close();              
            }
            LoginWin m_frmLogin = new LoginWin ();
            m_frmLogin .ShowDialog();
            m_frmLogin = null;
        }

public void ShowEmpInfo(Window objParentWin)
        {
            if (objParentWin != null)
            {
                objParentWin.Close();              
            }
            frmEmpInfo m_frmEmpInfo = new frmEmpInfo();
            m_frmEmpInfo.ShowDialog();
            m_frmEmpInfo = null;
        }

---IN login once credentials re verified iemp info will be shown if user click back button in the screen provided

main.Default.ShowLogin(this);







0
 

Author Comment

by:kmsmabu
ID: 24056854
In the default i will be having methods for all the windows in the application.
I am unable to find the memory leak as when i come back to login form the memory showed in task mgr for first itme is less than when i come from empindo screen
similar to all windows
0
 

Accepted Solution

by:
kmsmabu earned 0 total points
ID: 24141516
Hi unmeshdave:
Thanks for ur help.
Actually the issue was i was somehow retainig the reference of previous form i chkd in the stack tace and impliemnted a common page from which the forms of application will be loaded closing the previous form
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24141571
gud that u got the problem. Actually u can see my comment above with ID: 24041651. I have mentioned same problem.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question