我很难过 - 希望另一双眼睛能够看到我在这里失踪的东西。
我有一个我创建的wxpython程序(Windows 8.1上的Python 2.7.5下的wxpython 2.8.12.1)。程序的一部分是我创建的自定义控件,用于为用户表单生成透明的箭头形状按钮。
当我在表单中包含按钮时,它们的工作和外观完全符合预期;但是当我关闭顶级窗口时,程序崩溃了。我在Visual Studio中打开它,它告诉我堆已损坏,并说问题出在ntdll.dll中。确切的错误消息如下:
python.exe中0x00007FFC5FE912E0(ntdll.dll)的未处理异常:0xC0000374:堆已损坏(参数:0x00007FFC5FECDD40)。
我已经尝试禁用部分代码,但我似乎无法追查问题,我无法在Eclipse调试器中浏览它(它似乎发生在我的任何类获取wx之前.EVT_CLOSE事件)。如果程序中包含任何箭头,则会始终发生崩溃,否则不会发生。我怀疑它与我使用DC来绘制控件有关(我之前没有使用过很多),但这只不过是SWAG而且我真的不知道我做错了什么。
任何见解都会受到赞赏 - 我希望这是我看不到的非常明显的事情。
控件的代码如下(有点长,抱歉):
# ArrowButton.py
#
# Class module for ArrowButton control.
#
# Creates a button shaped like an arrow, pointing up (AB_STYLE_UPARROW, default) or down (AB_STYLE_DOWNARROW).
#
# This object generates wx.EVT_BUTTON events when clicked.
#
import wx
AB_STYLE_UPARROW = 256
AB_STYLE_DOWNARROW = 512
AB_MIN_WIDTH = 20
AB_MIN_HEIGHT = 12
AB_ACTIVE = 0
AB_DISABLED = 1
AB_PRESSED = 2
AB_PRESSED_OUTSIDE = 3
ROOT3 = 1.7320508075
class ArrowButton(wx.Control):
def __init__(self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.DefaultSize,
style = AB_STYLE_UPARROW, validator = wx.DefaultValidator, name = "ArrowButton"):
self.__style__ = style & (AB_STYLE_UPARROW + AB_STYLE_DOWNARROW)
if not (self.__style__ & AB_STYLE_UPARROW or self.__style__ & AB_STYLE_DOWNARROW):
raise ValueError("{} __init__ must specify AB_STYLE_UPARROW or AB_STYLE_DOWNARROW as one of its flags.".format(self.__class__.__name__))
if size == wx.DefaultSize:
calcsize = (AB_MIN_WIDTH, AB_MIN_HEIGHT)
else:
calcsize = size
wx.Control.__init__(self, parent = parent, id = id, pos = pos, size = calcsize,
style = (style & ~(AB_STYLE_UPARROW + AB_STYLE_DOWNARROW) | wx.TRANSPARENT_WINDOW | wx.NO_BORDER),
validator = validator, name = name)
self.SetSize(calcsize)
self.SetSizeHints(*calcsize)
self.SetMinSize(calcsize)
self.__activebitmap__ = None
self.__greyedbitmap__ = None
self.__pressedbitmap__ = None
self.__sensitivity__ = None
self.__mode__ = AB_ACTIVE
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.Bind(wx.EVT_SIZE, self.__OnSize__)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
self.Bind(wx.EVT_PAINT, self.__OnPaint__)
self.__GenerateBitmaps__()
def __GenerateBitmaps__(self):
size = self.GetClientSizeTuple()
Color = wx.SystemSettings.GetColour # This is a helper to shorten the text below.
bcFace = Color(wx.SYS_COLOUR_3DFACE)
bcLEdge = Color(wx.SYS_COLOUR_3DLIGHT)
bcDEdge = Color(wx.SYS_COLOUR_3DSHADOW)
self.__activebitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__greyedbitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__pressedbitmap__ = wx.EmptyBitmap(size[0], size[1])
self.__sensitivity__ = wx.EmptyBitmap(size[0], size[1])
memdc = wx.MemoryDC()
maskbitmap = wx.EmptyBitmap(size[0], size[1], depth = 1)
memdc.SelectObject(maskbitmap)
memdc.SetBackground(wx.WHITE_BRUSH)
memdc.Clear()
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), wx.BLACK, wx.BLACK, wx.BLACK)
memdc.SelectObject(wx.NullBitmap)
mask = wx.Mask(maskbitmap)
memdc.SelectObject(self.__sensitivity__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), wx.RED, wx.RED, None)
memdc.SelectObject(self.__activebitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcLEdge, bcDEdge)
memdc.SelectObject(self.__greyedbitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcLEdge, None)
memdc.SelectObject(self.__pressedbitmap__)
self.__GenerateTriangle__(memdc, (0, 0, size[0], size[1]), bcFace, bcDEdge, bcLEdge)
memdc.SelectObject(wx.NullBitmap)
self.__activebitmap__.SetMask(mask)
self.__greyedbitmap__.SetMask(mask)
self.__pressedbitmap__.SetMask(mask)
def __GenerateTriangle__(self, dc, where, face, bright, shadow):
if where[2] > ((2 * where[3]) / ROOT3): # The box is wider than it needs to be.
top = where[1]
bottom = where[1] + where[3] - 1
left = (where[0] + (where[2] / 2)) - int(where[3] / ROOT3)
right = (where[0] + (where[2] / 2)) + int(where[3] / ROOT3)
else: # The box is taller than it needs to be.
left = where[0]
right = where[0] + where[2] - 1
top = (where[1] + (where[3] / 2)) - int((where[2] * ROOT3) / 4)
bottom = (where[1] + (where[3] / 2)) + int((where[2] * ROOT3) / 4)
dc.SetBrush(wx.Brush(face, wx.SOLID))
dc.SetPen(wx.Pen(bright, 1, wx.SOLID))
if self.__style__ & AB_STYLE_UPARROW:
dc.DrawPolygon(points = [((left + right) / 2, top), (right, bottom), (left, bottom)])
if shadow:
dc.SetPen(wx.Pen(shadow, 1, wx.SOLID))
dc.DrawLine(left, bottom, right, bottom)
dc.DrawLine(right, bottom, (left + right) / 2, top)
else:
dc.DrawPolygon(points = [((left + right) / 2, bottom), (right, top), (left, top)])
if shadow:
dc.SetPen(wx.Pen(shadow, 1, wx.SOLID))
dc.DrawLine((left + right) / 2, bottom, right, top)
def DoGetBestSize(self):
return ((AB_MIN_WIDTH, AB_MIN_HEIGHT))
def Disable(self):
if self.__mode__ == AB_PRESSED or self.__mode__ == AB_PRESSED_OUTSIDE:
self.Unbind(wx.EVT_MOTION, self.__OnMouse__)
self.Unbind(wx.EVT_LEFT_UP, self.__OnMouse__)
elif self.__mode__ == AB_ACTIVE:
self.Unbind(wx.EVT_LEFT_DOWN)
self.Unbind(wx.EVT_LEFT_DCLICK)
self.__mode__ = AB_DISABLED
self.Refresh()
return wx.Control.Disable(self)
def Enable(self, enable = True):
if not enable:
return self.Disable()
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.__mode__ = AB_ACTIVE
self.Refresh()
return wx.Control.Enable(self, enable = enable)
def __OnMouse__(self, event):
if self.__mode__ == AB_DISABLED:
event.Skip()
return
where = event.GetPositionTuple()
memdc = wx.MemoryDC()
memdc.SelectObject(self.__sensitivity__)
inbounds = (memdc.GetPixel(*where).Get()[0] <> 0)
if self.__mode__ == AB_PRESSED:
if event.LeftUp(): # The button was released.
self.__mode__ = AB_ACTIVE
self.Refresh()
self.Unbind(wx.EVT_MOTION)
self.Unbind(wx.EVT_LEFT_UP)
self.Unbind(wx.EVT_MOUSE_CAPTURE_LOST)
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.ReleaseMouse()
newevent = wx.CommandEvent(commandType = wx.EVT_BUTTON.evtType[0], winid = self.GetId())
newevent.SetEventObject(self)
wx.PostEvent(self, newevent)
return
elif event.Dragging():
if not inbounds:
self.__mode__ = AB_PRESSED_OUTSIDE
self.Refresh()
elif self.__mode__ == AB_PRESSED_OUTSIDE:
if event.LeftUp():
self.__mode__ = AB_ACTIVE
self.Refresh()
self.Unbind(wx.EVT_MOTION)
self.Unbind(wx.EVT_LEFT_UP)
self.Unbind(wx.EVT_MOUSE_CAPTURE_LOST)
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.ReleaseMouse()
elif event.Dragging():
if inbounds:
self.__mode__ = AB_PRESSED
self.Refresh()
else: # Assume the button is in ACTIVE
if event.LeftDown() | event.LeftDClick():
if inbounds:
self.__mode__ = AB_PRESSED
self.Refresh()
self.Unbind(wx.EVT_LEFT_DOWN)
self.Unbind(wx.EVT_LEFT_DCLICK)
self.Bind(wx.EVT_LEFT_UP, self.__OnMouse__)
self.Bind(wx.EVT_MOTION, self.__OnMouse__)
self.Bind(wx.EVT_MOUSE_CAPTURE_LOST, self.__OnMouseCaptureLost__)
self.CaptureMouse()
return
event.Skip()
def __OnMouseCaptureLost__(self, event):
if self.__mode__ == AB_PRESSED or self.__mode__ == AB_PRESSED_OUTSIDE:
self.__mode__ = AB_ACTIVE
self.Unbind(wx.EVT_MOTION)
self.Unbind(wx.EVT_LEFT_UP)
self.Unbind(wx.EVT_MOUSE_CAPTURE_LOST)
self.Bind(wx.EVT_LEFT_DOWN, self.__OnMouse__)
self.Bind(wx.EVT_LEFT_DCLICK, self.__OnMouse__)
self.Refresh()
def __OnSize__(self, event):
size = event.GetSize()
if size[0] < AB_MIN_WIDTH:
size[0] = AB_MIN_WIDTH
if size[1] < AB_MIN_HEIGHT:
size[1] = AB_MIN_HEIGHT
event.Skip()
self.__GenerateBitmaps__()
def __OnPaint__(self, event):
dc = wx.PaintDC(self)
if self.__mode__ == AB_DISABLED:
dc.DrawBitmap(self.__greyedbitmap__, 0, 0, True)
elif self.__mode__ == AB_PRESSED:
dc.DrawBitmap(self.__pressedbitmap__, 0, 0, True)
else: # The button is ACTIVE or PRESSED_OUTSIDE.
dc.DrawBitmap(self.__activebitmap__, 0, 0, True)
答案 0 :(得分:1)
我想出了问题所在。
问题是我使用一个wx.Mask对象用于多个位图;我的猜测是,崩溃是由于在程序关闭时多次尝试取消分配掩码。
解决方案是为每个位图创建一个掩码。我的一部分认为这是浪费资源,因为掩模都是相同的 - 但另一方面,每个掩模通常是一个20x12的单色位图(我偶尔会重新进入20世纪80年代后期的编码思维模式)。
以下是受影响部分的修改代码:
{{1}}