Results 1 to 13 of 13

Thread: [RESOLVED] Problem with brushs

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Resolved [RESOLVED] Problem with brushs

    Hi guys, I got some problem in my project and I hope some one can help. The project is a control library. The user should be able to pass a drawing brush to the controls paint event, therefore I inherited the system PaintEventArgs class and added the Brush property to it.
    Now, the problem is that when a PathGridinentBrush is assigned to the Brush property of the custom-PaintEventArgs the memory usage is growing but if a SolidBrush is used the memory usage stays normal. Also, I want to mention that the custom-PaintEventArgs object is disposed after the use. Here is the class:
    vb Code:
    1. Public Class PaintEventArgs
    2.     Inherits System.Windows.Forms.PaintEventArgs
    3.  
    4.     Private _brush As Brush
    5.     Private _color As Color
    6.  
    7.  
    8.     Public Property Brush() As Brush
    9.         Get
    10.             'If brush is nothing then assign a solid brush
    11.             If Me._brush Is Nothing Then
    12.                 Me._brush = New SolidBrush(Me._color)
    13.             End If
    14.             Return Me._brush
    15.         End Get
    16.         Set(ByVal value As Brush)
    17.             Me._brush = value
    18.         End Set
    19.     End Property
    20.  
    21.  
    22.     Sub New(ByVal brush_color As Color, ByVal graphics As System.Drawing.Graphics, ByVal clipRect As System.Drawing.Rectangle)
    23.         MyBase.New(graphics, clipRect)
    24.         Me._color = brush_color
    25.     End Sub
    26.  
    27.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    28.         Try
    29.             If disposing AndAlso Me._brush IsNot Nothing Then
    30.                 Me._brush.Dispose()
    31.             End If
    32.         Finally
    33.             MyBase.Dispose(disposing)
    34.         End Try
    35.     End Sub
    36.  
    37. End Class
    Thanks for any help.
    VBDT

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Problem with brushs

    The only thing I can think of at the moment, that really is an unlikely source of an object not being disposed, is to take a look at the code for the PathGradientBrush Class and see if there's an obvious problem with it's dispose method. That would assume the unlikely situation in which a base library class was written with a logic error.

  3. #3

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Unhappy Re: Problem with brushs

    Quote Originally Posted by FourBlades
    The only thing I can think of at the moment, that really is an unlikely source of an object not being disposed, is to take a look at the code for the PathGradientBrush Class and see if there's an obvious problem with it's dispose method. That would assume the unlikely situation in which a base library class was written with a logic error.
    Hi FourBlades, thanks for the response. As I can see with a reflector tool the "PathGradientBrush" class doesn’t override the "Dispose" method of the base class (Brush), that means it uses the base class method. But the "SolidBrush" class overrides the base "Dispose" method and sets a Boolean member value of its own and than calls the base method.

    My tests show that there could be a possible bug in the "PathGradientBrush". Here is the test code I posted in MSDN forums if some one wants to test it himself.
    It seems I stuck with this problem for now.

  4. #4
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Problem with brushs

    Well then, assuming that it does not release some valuable resource, the next best thing would be to set each instance to Nothing and the GC do it's thing during the next couple of collections.

  5. #5
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Problem with brushs

    In fact if your program won't suffer for it, just call Collect after setting each instance to Nothing just to prevent the build-up.

  6. #6
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Problem with brushs

    Actually, there's no guarantee that will really help is there.
    It might, or it might not.

  7. #7

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Problem with brushs

    Quote Originally Posted by FourBlades
    In fact if your program won't suffer for it, just call Collect after setting each instance to Nothing just to prevent the build-up.
    Unfortunately, it will be very costly. At least, the app creates, uses and disposes five brushes every 100 millisecond. It will make the program very slow calling 50 times GC in a second, but thank you for the suggestion.

  8. #8
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Problem with brushs

    LinearGradientBrush also did not implement Dispose.

  9. #9
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Problem with brushs

    I did not scrutinize either class, but surely they did not forget to handle their resources. Maybe upon a close reading we'd see that it really wasn't needed in those classes?

    Are you sure you did not miss anything in your own code?
    It's just that I'd be very surprised if MS actually forgot to implement Dispose on these classes. If they did, it certainly needs to be fixed.

  10. #10

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Problem with brushs

    Quote Originally Posted by FourBlades
    I did not scrutinize either class, but surely they did not forget to handle their resources. Maybe upon a close reading we'd see that it really wasn't needed in those classes?

    Are you sure you did not miss anything in your own code?
    It's just that I'd be very surprised if MS actually forgot to implement Dispose on these classes. If they did, it certainly needs to be fixed.
    I created a test windows form project and posted the code in MSDN forums here. Check it out and try it, you will see that memory usage is constantly growing.

    Here is the screen shot of a test that is running for two hours and it's memory usage is constantly growing.
    Attached Images Attached Images  

  11. #11

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Problem with brushs

    I think I found the source of the bug. It depends which constructor of the “PathGridientBrush” will be used. The buggy constructor is the constructor that takes points. I posted the constructor and pointed out what is the problem in MSDN forums here. Now when I use the constructor that takes GraphicsPath object the memory used is normal. I guess MS got to fix this problem in the near future.

  12. #12
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Problem with brushs

    Oh, well if the memory is just growing by that much -- doesn't seem too extraordinary -- that's probably normal. I've seen and read where the runtime will consume more memory for an application at runtime by paging out some of the runtime overhead to make more room for the application. Maybe that's what's going on here.

  13. #13
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Problem with brushs

    Ok, well nevermind, all I saw was 10k and 34k my half blind eyes somehow missed the comma.

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