Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Is there a difference between these brush calls?

I'm receiving some buffer errors occasionally in my application when run for too long. I was reading on some other questions that this could be caused by GDI objects not being disposed of. So I am going through the code to clean it up and make sure all instances are being disposed of and notice in some places I have a 'Using' statement and in others I reference the system object directly.

My question: Are the following statements equivalent or will the bottom statement leave an un-disposed GDI object after the form is closed?

First - Using statement
Using DRedBrush As Brush = Brushes.DarkRed
    g.DrawString("FOO", Font, DRedBrush, 100.0F, 100.0F)
End Using

Second - Not Using statement
g.DrawString("BAR", Font, Brushes.DarkRed, 100.0F, 100.0F)

If relevant, the application is a Winforms application using vb.net.

Actual Exception occuring:

Unhandled Exception::  System.InvalidOperationException: BufferedGraphicsContext cannot be disposed of because a buffer operation is currently in progress.
  at System.Drawing.BufferedGraphicsContext.Dispose(Boolean disposing)
  at System.Drawing.BufferedGraphicsContext.Dispose()
  at System.Drawing.BufferedGraphicsContext.AllocBufferInTempManager(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
  at System.Drawing.BufferedGraphicsContext.Allocate(IntPtr targetDC, Rectangle targetRectangle)
  at System.Windows.Forms.Control.WmPaint(Message& m)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
  at System.Windows.Forms.ContainerControl.WndProc(Message& m)
  at System.Windows.Forms.UserControl.WndProc(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Comments