Microsoft 开发人员网络 > 论坛主页 > Windows Presentation Foundation (WPF) > Memory is never released. Can't understand even with a profiler.
提出问题提出问题
 

已答复Memory is never released. Can't understand even with a profiler.

  • 2009年6月27日 19:43XavierP 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi,

    call the following code several times : Memory is NEVER released. Why ?? Thnaks !
            void SignPicture(string filePath)
            {
                DoIncrust(filePath);

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

                File.Delete(filePath);

                File.Move("c:/result.jpg", filePath);
                return;
            }

            private static void DoIncrust(string filePath)
            {

                MemoryStream mem;
                byte[] buffer = File.ReadAllBytes(filePath);
                // by reading the data into an in-memory buffer, we prevent the file from being read in the UI thread
                // -- which speeds up access dramatically!
                mem = new MemoryStream(buffer);

                BitmapImage original = new BitmapImage();
                original.BeginInit();
                original.CacheOption = BitmapCacheOption.None;
                original.StreamSource = mem;
                original.EndInit();

                MemoryStream mem2;
                byte[] buffer2 = File.ReadAllBytes("c:/signature.png");
                mem2 = new MemoryStream(buffer2);

                BitmapImage signature = new BitmapImage();
                signature.BeginInit();
                signature.CacheOption = BitmapCacheOption.None;
                signature.StreamSource = mem2;
                signature.EndInit();

                Image ori = new Image();
                ori.Source = original;
                Image sign = new Image();
                sign.Source = signature;

                Grid signaGrid = new Grid();

                double percent = 0.50;

                double mywidth, myheight;

                ComputeWidthHeight(original, out mywidth, out myheight);

                double ratio = (double)mywidth / (double)myheight;
                double vwidth = percent;
                double vheight = vwidth / ratio;

                double otherwidth = 1.0 - vwidth;
                double otherheight = 1.0 - vheight;

                RowDefinition rd = new RowDefinition();
                rd.Height = new GridLength(otherheight, GridUnitType.Star);
                RowDefinition rd1 = new RowDefinition();
                rd1.Height = new GridLength(vheight, GridUnitType.Star);
                signaGrid.RowDefinitions.Add(rd);
                signaGrid.RowDefinitions.Add(rd1);

                ColumnDefinition cd = new ColumnDefinition();
                cd.Width = new GridLength(otherwidth, GridUnitType.Star);
                ColumnDefinition cd1 = new ColumnDefinition();
                cd1.Width = new GridLength(vwidth, GridUnitType.Star);
                signaGrid.ColumnDefinitions.Add(cd);
                signaGrid.ColumnDefinitions.Add(cd1);

                // Add the image
                Grid.SetRow(ori, 0);
                Grid.SetColumn(ori, 0);
                Grid.SetRowSpan(ori, 2);
                Grid.SetColumnSpan(ori, 2);
                signaGrid.Children.Add(ori);

                // Add the signature image
                Grid.SetRow(sign, 1);
                Grid.SetColumn(sign, 1);
                signaGrid.Children.Add(sign);

                // Render everything
                RenderTargetBitmap bmp = new RenderTargetBitmap(original.PixelWidth, original.PixelHeight,
                    original.DpiX, original.DpiY, PixelFormats.Pbgra32);

                signaGrid.Measure(new Size(original.Width, original.Height));
                signaGrid.Arrange(new Rect(0, 0, original.Width, original.Height));

                bmp.Render(signaGrid);

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                BitmapFrame bf = BitmapFrame.Create(bmp);

                encoder.Frames.Add(bf);
                FileStream stream;
                stream = new FileStream("c:/result.jpg", FileMode.Create);
                encoder.Save(stream);
                stream.Flush();
                stream.Close();
                stream.Dispose();
                ori.Source = null;
                sign.Source = null;
                signaGrid.RowDefinitions.Clear();
                signaGrid.ColumnDefinitions.Clear();
                bmp.Clear();
                buffer = null;
                buffer2 = null;
                mem.Dispose();
                mem2.Dispose();
                bf = null;
                encoder.Frames.Clear();
            }

            private static void ComputeWidthHeight(BitmapImage original, out double mywidth, out double myheight)
            {
                if (original.PixelWidth > original.PixelHeight)
                {
                    // Paysage
                    mywidth = original.PixelWidth;
                    myheight = original.PixelHeight;
                }
                else
                {
                    // Portrait
                    mywidth = original.PixelHeight;
                    myheight = original.PixelWidth;
                }
            }

答案

全部回复

  • 2009年6月27日 20:03Mark SalsberyMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Found another filed issue on Connect - this one states a leak will be fixed in future versions...

    WPF RenderTargetBitmap still leaking, and badly!

    Mark Salsbery Microsoft MVP - Visual C++
  • 2009年6月27日 20:27XavierP 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    This one is a real showstopper to me. Any workarounds ?

    It's going hard to wait for next release to get it fixed !!

    HELP !

    Thanks !
  • 2009年6月27日 21:08Ray M_ 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    KB967634 might just be the fix you need.
  • 2009年6月28日 18:14XavierP 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Ray,

    May be... But I'm doing WPF on my leisure time and can't afford to pay for any support ! It seems the .dll are not free to download.

    And I would prefer a workaround rather than this...

    Thanks anyway !
  • 2009年6月28日 22:40Ray M_ 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    If there was a work around it would have been mentioned in the KB article, you usually have to go though support to get the hotfixes cause well they are usually not as well tested as service packs and using serveral fixes that all replace the same dll can give undesired results. support will probably have a better talk explaining the risks but as long as you understand hotfixes are not risk free go nuts
  • 2009年6月30日 11:12Bruce.ZhouMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi XavierP,

    As the KB indicates, the hotfix still needs additional test which I think is the reason the download link is not directly in that article. Ray is right, hotfix is not risk free. Besides, the article also recommends waiting for next release if you are not severely affect by this problem.

    Best regards,
    Bruce Zhou
    Please mark the replies as answers if they help and unmark if they don't.
  • 2009年7月1日 15:15XavierP 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Bruce,

    I'm impacted but I'll disable some features in my app.  :-(

    When is expected a another service pack for .Net 3.5 ? I'm still a big fan of WPF but this bug really annoys me.

    Thanks.
  • 2009年7月2日 2:28Bruce.ZhouMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi XavierP,

    Totally understand your feeling. Sorry for the trouble this issue brought. Currently, I do not know when the next service pack will be released either.

    Actually, I have the hotfix in my machine, but since it needs additional tests, I suggest you wait for the next service pack. If you really can't wait, maybe I can send it to you through email.  Besides, I need to know the version of your operating system.

    Best regards,
    Bruce Zhou



    Please mark the replies as answers if they help and unmark if they don't.
  • 2009年7月4日 17:00XavierP 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi Bruce,

    I'm using  Vista French edition 32 bits.
    When is the next service pack scheduled ?

    Thanks.
  • 2009年7月5日 2:09Bruce.ZhouMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi XavierP,

    Sorry, I've no idea with the plan of the service pack now. But I think it will not be long.

    Best regards,
    Bruce Zhou
    Please mark the replies as answers if they help and unmark if they don't.