C#winform内存溢出异常

new其他的不会报内存溢出异常,new窗口就报内存溢出异常。

可能新窗体有死循环,用调试模式看最终得到什么错误提示。

分配的内存过多,或者是有无限递归调用,贴出代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Lyrics : Form
{
public Lyrics()
{
InitializeComponent();
}
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();

    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

    protected override void WndProc(ref Message m)
    {
        Console.WriteLine(m);
        base.WndProc(ref m);
    }

    public string songname;
    public string du;
    public Form1 f1;
    public int abc;
    public List<Lrc> lrcs = new List<Lrc>();
    LrcMethod l = new LrcMethod();
    int question;
    Thread thread;
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;
            return cp;
        }
    }


    /// <summary>
    /// 是否有网络的方法
    /// </summary>
    /// <returns></returns>
    private bool IsValidateOnline()
    {
        try
        {
            Uri MyUri = new Uri("Http://www.baidu.com");
            WebRequest wb = WebRequest.Create(MyUri);
            wb.Proxy = new WebProxy();
            WebResponse wsp = wb.GetResponse();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
    public int yn;  //传到Form1的label判断
    DownSongerImg down = new DownSongerImg();
    public void xx()
    {
        http h = new http();
        kugou k = new kugou();           
        String url;
        bool ccaa = IsValidateOnline();
        if(lrcs!=null)
        lrcs.Clear();
        du = du.Substring(0, 3);
        if (File.Exists("Lyrics\\"+songname + ".lrc"))
        {
            if (ccaa)
            {
                if (File.Exists(@"photo\" + f1.author + ".jpg"))
                {
                    this.BackgroundImage = Image.FromFile(@"photo\" + f1.author + ".jpg");
                }
                else
                {
                    url = h.getUrl(h.getJsonText(k.getimg(f1.author)));   //k.getimg是替换文字,h.getJsonText是接收Json格式字符串,h.getUrl是从Json获取下载地址
                    if (url != null)
                    {
                        down.path = @"photo\" + f1.author + ".jpg";
                        down.Down(url);
                    }
                    else
                    {
                        this.BackgroundImage = Image.FromFile(@"D:\迅雷下载\风景图.jpg"); 
                    }
                }
            }
            else
            {
                if (File.Exists(@"photo\" + f1.author + ".jpg"))
                {
                    this.BackgroundImage = Image.FromFile(@"photo\" + f1.author + ".jpg");
                }
                else
                {
                    this.BackgroundImage = Image.FromFile(@"D:\迅雷下载\风景图.jpg");
                }
            }
            lrcs = l.loadfile(songname);
            layoutLrc(lrcs);              
            yn = 0;
            return;
        }
        if (ccaa == false)
        {
            yn = 1;
            return;
        }
        else
        {
            if (File.Exists(@"photo\" + f1.author + ".jpg"))
            {
                this.BackgroundImage = Image.FromFile(@"photo\" + f1.author + ".jpg");
            }
            else
            {
                url = h.getUrl(h.getJsonText(k.getimg(f1.author)));   //k.getimg是替换文字,h.getJsonText是接收Json格式字符串,h.getUrl是从Json获取下载地址
                if (url != null)
                {
                    down.path = @"photo\" + f1.author + ".jpg";
                    down.Down(url);
                }
                else
                {
                    this.BackgroundImage = Image.FromFile(@"D:\迅雷下载\风景图.jpg");
                }
            }
            lrcs = l.LoadLrc(songname, du);
            if (lrcs == null)
            {
                if (question == 1)
                {
                    question = 0;
                    return;
                }
                thread = new Thread(new ThreadStart(NewMethod));
                thread.Start();
                panel1.Controls.Clear();
                yn = 0;
                return;
            }
            layoutLrc(lrcs);
            yn = 0;
            GC.Collect();
        }
    }

    private void NewMethod()
    {
        question = 1;
        MessageBox.Show("不好意思,没有找到这首歌的歌词");       
    }
    public void layoutLrc(List<Lrc> lrc)
    {
        int j = 4;
        panel1.Controls.Clear();
        panel1.AutoScrollMargin = new System.Drawing.Size(0, 200);
        for (int i = 0; i < lrc.Count; i++)
        {
            j++;
            Label label = new Label();
            label.BackColor = Color.Transparent;
            label.Location = new Point(panel1.Location.X, 30 * j);
            label.Font = new System.Drawing.Font("新宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            label.Text = lrc[i].Value;
            label.Size = new System.Drawing.Size(panel1.Width, 30);
            label.AutoSize = false;
            label.Tag = lrc[i].Key;
            label.ForeColor = Color.FromArgb(244, 244, 244);
            label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            panel1.Controls.Add(label);
        }

    }
    Label l1 = new Label();
    private void label3_TextChanged(object sender, EventArgs e)
    {        
        foreach (Control item in panel1.Controls)
        {
            Label l = new Label();
                l = (Label)item;
                string labelstr = label1.Text;
                int a = Convert.ToInt32(l.Tag.ToString().Replace(":", ""));
                int b=0;
                try
                {
                    b = Convert.ToInt32(labelstr.Replace(":", ""));
                }
                catch (Exception)
                {
                    Console.WriteLine("ABC");
                }
                if (a == b)
                {
                    l1.ForeColor = Color.FromArgb(244, 244, 244);
                    l1.Font = new System.Drawing.Font("新宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    l.ForeColor = Color.FromArgb(249, 216, 129);
                    l.Font = new System.Drawing.Font("新宋体", 11.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    l1 = l;
                    panel1.ScrollControlIntoView(l);
                }
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            if (thread.ThreadState == ThreadState.Stopped)
            {
                thread.Abort();
            }
        }
        catch (Exception)
        {
            Console.WriteLine("ABC");
        }
    }
    private void button1_MouseMove(object sender, MouseEventArgs e)
    {
        Button bt = (Button)sender;
        bt.BackgroundImage = Image.FromFile(@"Image\button2.png");           
    }
    private void button1_MouseLeave(object sender, EventArgs e)
    {
        Button bt = (Button)sender;
        bt.BackgroundImage = Image.FromFile(@"Image\button1.png");
    }
    public Exist exit = new Exist();  
    private void Lyrics_Load(object sender, EventArgs e)
    {
        exit.ly = this;
        string url=@"D:\IQIYI Video\cur\cur\光标\Help_Gea.cur";
        down.lyric = this;
        button1.Cursor = new Cursor(url);
        button2.Cursor = new Cursor(url);
        label1.Visible = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f1.Show();
        this.Hide();
        abc = 1;
    }

    private void panel2_MouseMove(object sender, MouseEventArgs e)
    {
        ReleaseCapture();
        //发送消息,0x112代表系统命令,0xF012代表移动
        SendMessage(this.Handle, 0x0112, 0xF012, 0);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

}

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

namespace WindowsFormsApplication2
{
public partial class Exist : Form
{
public Exist()
{
InitializeComponent();
}

public Form1 f1;
string[] number = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < number.Length; i++)
{
if (textBox2.Text == number[i])
{
textBox2.Text = "0" + number[i];
}
if (textBox3.Text == number[i])
{
textBox3.Text = "0" + number[i];
}
}
if (textBox2.Text != "")
{
if (textBox3.Text == "")
{
textBox3.Text = "00";
}
}
count = 0;
timer2.Start();
}

    private void button2_Click(object sender, EventArgs e)
    {
        timer2.Stop();
        this.TopMost = false;
    }

    private void Exist_Load(object sender, EventArgs e)
    {
        textBox1.Text = DateTime.Now.ToShortTimeString();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        textBox1.Text = DateTime.Now.ToShortTimeString();
    }
    double size;
    private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 0x20)
        {
            e.KeyChar = (char)0;
        }
        if (e.KeyChar > 0x20)
        {
            try
            {
                size = double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                if (textBox2.SelectionLength == 1 || textBox2.SelectionLength == 2)
                {
                    return;
                }
                if (size > 23)
                {
                    e.KeyChar = (char)0;
                }
            }
            catch
            {
                e.KeyChar = (char)0;   //处理非法字符  
            }
        }  
    }

    private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 0x20)
        {
            e.KeyChar = (char)0;
        }
        if (e.KeyChar > 0x20)
        {
            try
            {
                size = double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                if (textBox3.SelectionLength == 1 || textBox3.SelectionLength == 2)
                {
                    return;
                }
                if (size > 59)
                {
                    e.KeyChar = (char)0;
                }
            }
            catch
            {
                e.KeyChar = (char)0;   //处理非法字符  
            }
        }  
    }
    int count;
    public Lyrics ly=new Lyrics(); 
    private void timer2_Tick(object sender, EventArgs e)
    {
        if (count != 1)
        {
            if (DateTime.Now.Minute == (Convert.ToInt32(textBox3.Text) - 1))
            {
                count++;
                this.TopMost = true;
                if (ly.Visible == false)
                     ly.Hide(); 
                this.Show();
                MessageBox.Show("还有一分钟,播放器将退出","温馨提示");                  
            }
        }
        if (textBox1.Text == textBox2.Text + ":" + textBox3.Text)
        {
            Application.Exit();
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        pictureBox1.Image = Image.FromFile(@"Image/exists2.png");
    }

    private void pictureBox1_MouseLeave(object sender, EventArgs e)
    {
        pictureBox1.Image = Image.FromFile(@"Image/exists1.png");
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        f1.Show();
        this.Hide();
    }

}

}

一启动就报这个图片说明