diff --git a/etg/graphics.py b/etg/graphics.py index 1e2131b0..28808563 100644 --- a/etg/graphics.py +++ b/etg/graphics.py @@ -208,11 +208,24 @@ def run(): markCreateFactories(c) c.abstract = True - for m in c.find('CreateContext').all(): - for p in m.items: - if 'DC' in p.name or p.name == 'image': - p.keepReference = True - c.find('CreateContextFromImage.image').keepReference = True + + # The KeepReference annotation doesn't work for us in this case, as it will + # hold the reference in the renderer object, but it is better to hold the + # reference in the returned context object instead. Otherwise there is still + # some possibility that the held DC will be destroyed before the context. + c.addPyCode("""\ + def _ctx_hold_ref(f): + from functools import wraps + @wraps(f) + def wrapper(self, obj): + ctx = f(self, obj) + ctx._obj = obj + return ctx + return wrapper + GraphicsRenderer.CreateContext = _ctx_hold_ref(GraphicsRenderer.CreateContext) + GraphicsRenderer.CreateContextFromImage = _ctx_hold_ref(GraphicsRenderer.CreateContextFromImage) + GraphicsRenderer.CreateContextFromUnknownDC = _ctx_hold_ref(GraphicsRenderer.CreateContextFromUnknownDC) + """) # TODO: support this? c.find('CreateContext').findOverload('wxEnhMetaFileDC').ignore()