• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

Memory leak in webbrowser.DocumentText

My program recursively inputs links into a webbrowser control and downloads the html to a folder. I ran the Visual Studio profiler on my app and let it run for a few hours and when I came back it was using 600mb+ of memory. My app isn't supposed to be saving anything into memory, only to disk. Here is the relevant code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Default
{
    public void ImportHTML1()
    {
            if (!data.processed)
            {
                for (int i = start; i < end; i++)
                {
                    for (int j = 0; j < data.people.Tables[i].Rows.Count; j++ )
                    {
                        using (var wb = new webbrowser(data.people.Tables[i].Rows[j]["Name"].ToString(), data.people.Tables[i].Rows[j]["ID"].ToString(), "A1"))
                            wb.ShowDialog();
                    }
                }
    }
}

public partial class webbrowser : Form
    {
        String name, url, id, fileName, what;
        
        // Accpet a list of sites in a DataTable
        public webbrowser(String nm, string st, String wh)
        {
            name = nm;
            id = st;
            what = wh;
            InitializeComponent();
        }

        private void webbrowser_Load(object sender, EventArgs e)
        {
            if (what == "A1")
            {
                fileName = name;
                url = "http://192.168.1.1/id=?" + id;

                ctrlWebBrowser.Navigate(url);

                // Pause for 10 seconds to allow the page to load, so we can save it
                using (var to = new timeout(5000))
                    to.ShowDialog();

                ctrlWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ctrlWebBrowser_DocumentCompleted);

                //GC.Collect();
                //GC.WaitForPendingFinalizers();

                this.Close();
            }
        }

        // When the page is done loading..
        private void ctrlWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Store the html
            using (FileStream fs = new FileStream(data.openFileName + @"\" + fileName + ".html", FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter((fs)))
                {
                    // Saves the file to disk
                    sw.WriteLine(ctrlWebBrowser.DocumentText);
                    ctrlWebBrowser.DocumentText = null;
                }
            }

            this.Dispose();
        }
    }

Open in new window

2.png
0
RicardoLovato
Asked:
RicardoLovato
  • 2
  • 2
1 Solution
 
anarki_jimbelCommented:
Memory leaks are always tricky. Try the following.

You have the line:P
               ctrlWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ctrlWebBrowser_DocumentCompleted);

Not quite sure what is the best place in your code but I'd advise to de-attach the handler. This may be a source of the leak.

0
 
RicardoLovatoAuthor Commented:
Should I just add the event as a function and call it? The program is no good unless it can save off the html.
0
 
anarki_jimbelCommented:
Not 100% how I'd do that. Probably Id fire some custom event to signal about saving the stuff and would handle removing event handlers etc. You may try to de-attach handler after the line:
                  ctrlWebBrowser.DocumentText = null; - it's inside the handling method itself...
0
 
RicardoLovatoAuthor Commented:
I ended up switching to the Gecko (Mozilla) browser and it fixed my problem so there is probably some memory leak in internet explorer.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now