none
内存泄漏问题

    问题

  • 下面的代码中这句有内存泄漏错误using (var ergebnis = ResizeImage(Bitmap.FromFile(f), 1, 1)) ,请问是什么原因? 有什么东西没有释放吗?

    SQLiteConnection.CreateFile("datenbank.sqlite");
    
    m_dbConnection =
    
    new SQLiteConnection("Data Source=datenbank.sqlite;Version=3;");
    
    m_dbConnection.Open();
    
    string sql = "CREATE TABLE verbindungen (farbeR NUMERIC, farbeG NUMERIC, farbeB NUMERIC, Dateiname TEXT)";
    
    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
    
    command.ExecuteNonQuery();
    
    int k = 0;
    
    foreach (var f in files)
    
    {
    
        k++;
    
        this.Invoke((MethodInvoker)delegate
    
        {
    
            this.Text = "Datei "+k+ " von "+files.Length+", Fehler: " + fehler;
    
        });
    
     
    
        using (FileStream file = new FileStream(f, FileMode.Open, FileAccess.Read))
    
        {
    
            using (var ergebnis = ResizeImage(Bitmap.FromFile(f), 1, 1))
    
            {
    
                Color ds = ergebnis.GetPixel(0, 0);
    
                //
    
                using (var command2 = new SQLiteCommand())
    
                {
    
                    sql = "INSERT INTO verbindungen VALUES(" + ds.R + ", " + ds.G + ", " + ds.B + ", '" + f + "')";
    
                    command2.CommandText = sql;
    
                    command2.Connection = m_dbConnection;
    
                    command2.ExecuteNonQuery();
    
                }
    
     
    
            }
    
        }
    
     
    
        try
    
        {
    
     
    
        }
    
        catch (Exception)
    
        {
    
            this.Invoke((MethodInvoker)delegate
    
            {
    
                fehler++;
    
            });
    
            //
    
        }
    
        //MessageBox.Show(f + ": " + abstand(ds,vergleichsfarbe));
    
    }
    
     
    
    public static Bitmap ResizeImage(Image image, int width, int height)
    
    {
    
        var destRect = new Rectangle(0, 0, width, height);
    
        var destImage = new Bitmap(width, height);
    
     
    
        destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    
     
    
        using (var graphics = Graphics.FromImage(destImage))
    
        {
    
            graphics.CompositingMode = CompositingMode.SourceCopy;
    
            graphics.CompositingQuality = CompositingQuality.HighSpeed;
    
            graphics.InterpolationMode = InterpolationMode.Low;
    
            graphics.SmoothingMode = SmoothingMode.None;
    
            graphics.PixelOffsetMode = PixelOffsetMode.None;
    
     
    
            using (var wrapMode = new ImageAttributes())
    
            {
    
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
    
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
    
            }
    
        }
    
        return destImage;
    }

    2016年2月29日 12:56

答案

  • 你好,

    请看这一行代码

    using (var ergebnis = ResizeImage(Bitmap.FromFile(f), 1, 1))

    Bitmap.FromFile 新建了一个Bitmap, 却没有那里释放它。建议修改成这样。

    using (var image = Bitmap.FromFile(f))
    using (var ergebnis = ResizeImage(image, 1, 1))

    Best regards,

    Cole Wu

    2016年3月1日 14:25
    版主