Need debugging help for InvalidOperationException
Posted on 2005-03-28
I have a program that I have simplified, but still I am getting an intermitiant crash with a NullPointer Exception.
I have a user control derived class that displays a changing bitmap. In the class, I have implemented a timer using System.Timers.Timer and in the Elapsed event, I call the Refresh method on the UserControl. In the paint delegate defined on the class, I check to see if the bitmap data member of the class is null or not. If not, I call the passed in PaintEventArgs Graphics object DrawImage method to draw the bitmap.
But for some reason, I eventually get a NullPointerException in the DrawImage call and when I examine the bitmap and graphics objects, I see in the Quick Watch panel the value of InvalidOperationException. I have checked the threads running and I have checked my dispose method and I am sure the object with the paint delegate being invoked is a valid object and the bitmap was just set on the object. I even tried cloning the bitmap to be sure it is a "fresh" one so it will not get deleted out from under me.
I am at my wits end to figure out how to determine what is wrong. I dont know what else to do to figure out why the bitmap is becoming invalid. Can anyone tell me what might be going wrong and how I can fix it?
Is there a problem with running a worker thread and calling the Refresh method on myself?
Is there a way I can tell if the Graphics object is invalid? Its not null, but it is also not valid? How can I trace what caused the class to get into the broken state it seems to be in?
I put in a global unhandled exception hander and if I run the program outside of the debugger, I get the following exception:
The object is currently in use elsewhere.
at System.Drawing.Graphics.EndContainer(GraphicsContainer container)
at System.Windows.Forms.DibGraphicsBufferManager.ReleaseBuffer(GraphicsBuffer buffer)
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.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I dont understand why this is happening or how to narrow down what is the cause. There are no methods in my code involved with this error. Can someone clearify what this means?