I'm getting strange errors in a custom panel that double-buffers its drawing routines. The basic routines, which are based on a wx double buffering demo, are:
def OnSize(self, event):
(w,h) = self.GetClientSize()
self.bitmapbuffer = wx.EmptyBitmap(w, h)
def OnPaint(self, event):
if USE_BUFFERED_DC:
dc = wx.BufferedPaintDC(self, self.bitmapbuffer)
else:
dc = wx.PaintDC(self)
dc.DrawBitmap(self.bitmapbuffer, (0,0))
def UpdateDrawing(self):
(w,h) = self.GetClientSizeTuple()
self.drawing_mutex.acquire()
if USE_BUFFERED_DC:
dc = wx.BufferedDC(wx.ClientDC(self), self.bitmapbuffer)
self.DrawBoard(dc)
else:
# update the buffer
dc = wx.MemoryDC()
dc.SelectObject(self.bitmapbuffer)
self.DrawBoard(dc)
# update the screen
wx.ClientDC(self).Blit((0, 0), (w, h), dc, (0, 0))
self.drawing_mutex.release()
Notice the flag used by the demo to turn buffering on or off, yet the drawings (and errors) appear to be the same either way.
Since the actual application has several threads that could potentially call UpdateDrawing at any time, there's a mutex to regulate access. Yet despite the mutex, I get the following exception:
Traceback (most recent call last):
File "boardcanvas.py", line 492, in OnPaint
dc = wx.BufferedPaintDC(self, self.bitmapbuffer)
File "C:\Python23\Lib\site-packages\wx\gdi.py", line 3079, in __init__
newobj = _gdi.new_BufferedPaintDC(*args, **kwargs)
wx.core.PyAssertionError: C++ assertion "wxAssertFailure" failed in ..\..\src\ms
w\dcmemory.cpp(133): Couldn't select a bitmap into wxMemoryDC
Occasionally the exception will say recommend deleting the previous DC or using SelectObject(wx.NullBitmap).
Sometimes, the window will just freeze even with no exception. What could be causing this problem? Any help would be greatly appreciated.