Results 1 to 3 of 3

Thread: [RESOLVED] Leakey wpf

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Resolved [RESOLVED] Leakey wpf

    I have an issue where I am using WPF to apply a System.Windows.Media.Effects.Effect to a System.Drawing.Bitmap using the following:

    vb Code:
    1. Partial Public Class ImageFilter
    2.  
    3.     Public Sub ApplyWPFEffect(ByVal Effect As System.Windows.Media.Effects.Effect)
    4.         OnFilterStarted(r)
    5.         Dim ImageSource As New System.Windows.Media.Imaging.BitmapImage()
    6.         Using ms As New IO.MemoryStream()
    7.             b.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
    8.             ImageSource.BeginInit()
    9.             ImageSource.StreamSource = ms
    10.             ImageSource.EndInit()
    11.             Dim FormImage As New System.Windows.Controls.Image()
    12.             FormImage.Effect = Effect
    13.             FormImage.Source = ImageSource
    14.             Dim ImageSize = New System.Windows.Size(b.Width, b.Height)
    15.             FormImage.Measure(ImageSize)
    16.             Dim renderingRectangle As New System.Windows.Rect(ImageSize)
    17.  
    18.             'QUITE LEAKY >>>
    19.             FormImage.Arrange(renderingRectangle)
    20.  
    21.  
    22.             Dim enc As New System.Windows.Media.Imaging.PngBitmapEncoder
    23.  
    24.             'XTREMELY LEAKY!!!! >>>
    25.             Dim bmpSource As New System.Windows.Media.Imaging.RenderTargetBitmap(b.Width, b.Height, 96, 96, System.Windows.Media.PixelFormats.Pbgra32)
    26.  
    27.             bmpSource.Render(FormImage)
    28.             enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmpSource))
    29.             Using msOut As New IO.MemoryStream
    30.                 enc.Save(msOut)
    31.                 Using bOut As New Bitmap(msOut)
    32.                     Using g As Graphics = Graphics.FromImage(b)
    33.                         g.Clear(Color.Transparent)
    34.                         g.DrawImage(bOut, 0, 0, bOut.Width, bOut.Height)
    35.                     End Using
    36.                 End Using
    37.             End Using
    38.         End Using
    39.         OnFilterFinished()
    40.     End Sub
    41.  
    42. End Class

    If I loop this code the memory keeps growing ...
    What could be going on?

    EDIT: The really leaky lines now have comments before them...
    I have also tried methods suggested online about calling clear and freeze on it with no success.

    Thanks in advance,
    Kris

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Leakey wpf

    Is there some reason you don't trust the GarbageCollector?

    Local variables like FormImage and bmpSource go out of scope at the end of the sub. Any memory reserved due to the declarations or method calls is marked for collection the next time the GarbageCollector runs. For efficiency reasons, the GarbageCollector does not run until there is a request for memory that cannot be satisfied (it enjoys a bulk discount, so to speak.) This means you can ignore any apparent peaks or accumulation in "memory consumed", however you measure that. Trying to force garbage collection with GC.Collect tends to reduce the efficiency of garbage collection and generally should only be done for diagnostic purposes. EDIT: see here for a detailed article.

    BB

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: [RESOLVED] Leakey wpf

    I know this is an old post ... but it is not that I don't trust the garbage collector ... it is that the objects were not getting disposed of ... so even when the gc collects there was still a lot of memory used... and others had found this too!

    Anyway ... I can't remember where I found the article about fixing this .. but basically the solution is to put the whole thing in a thread dispatcher

    Kris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width