0

Ants Memory Profiler を理解しようとしています。私が抱えている問題は、メモリリークのある簡単なアプリケーションがないことです。

Redgate (QueryBee) のサンプルを使用しましたが、私の好みに合わせたものでした。そのための簡単なアプリが必要です。

作成しようとしましたが、うまくいきません。それは機能していません。メモリリークはありません。呼び出されたフォームを破棄せずに ShowDialog を呼び出すと、メモリリークが発生することについて読みましたが、ここではそうではありません。

VS2010 と .NET 4.0 を使用しています

私は非常に一般的な問題に特に興味があります。

これが私がこれまでに持っているものです。メモリリークを取得できますか?:

メインフォーム

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MemoryLeakTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SubForm formToCall = new SubForm();
            formToCall.ShowDialog();
        }
    }
}

サブフォーム

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace MemoryLeakTest
{
    public partial class SubForm : Form
    {


        public SubForm()
        {
            InitializeComponent();
        }

        private void SubForm_Load(object sender, EventArgs e)
        {

            ArrayList persons = new ArrayList();

            for (int i = 0; i <= 50000; i++)
            {
                var person = new {
                    Name = "1 SchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorsch",
                    LastName = "KluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluni", 
                    Age = 50,
                    City = "LondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondon",
                    Zip = "223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301", 
                    Index = i 

                };
                persons.Add(person);
            }

            dataGridView1.DataSource = persons;
        }

        private void SubForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //this.Dispose();
        }


    }
}
4

1 に答える 1

1

これをサブフォームに追加します

public void mouse_handler(object sender, MouseEventArgs e)
{

}

そして、これを行うためにMainFormを変更します

private void button1_Click(object sender, EventArgs e)
{
     SubForm formToCall = new SubForm();
     this.MouseClick += new MouseEventHandler(formToCall.mouse_handler);
     formToCall.ShowDialog();
}

これで、サブフォームを.Dispose()するかどうかは関係ありませんが、「リーク」が発生します。MainFormは、マウスイベントハンドラーを介してサブフォームへの参照を無期限に保持します。このハンドラーは登録解除されないため、基本的にはイベントを受信するユーザーのリストにすぎません。

Antはこれを追跡するのに役立ちますが、手動ではなく、オブジェクトがまだ生きていてルートから参照されていることを示します。これらのオブジェクトはどこからも参照されるべきではないことを発見する必要があります。Antsも警告などを生成できると思います。.Disposed()であるが、まだどこかで参照されているオブジェクトが見つかったとき。

于 2011-10-08T19:44:27.327 に答える