[wxPython] Repost: Memory management of wxOGLConstraints

Just a repost of my earlier message this time in plain text. Before it was a mail created by the Solaris Mail Tool with an attachement in plain text, which caused troubles reading it.

Dear all,

I wish to build an application which is able to change the type of a wxOGLConstraint during run time. Trying this I found the following problems. They are probably related to the memory management of wxPython and C objects.

I am running wxPython 2.2.2 and Python 2.0 on a Windows NT machine.

1. Problem

The following procedure fails:
a) assigning a new created wxOGLConstraint to a Python variable
b) adding the constraint to the composite shape
c) recalculating the shape now is OK, until
d) the Python variable goes out of scope
e) all methods trying to access the constraint for calculation fail with a
   Windows NT error message

2. Problem

To go around this problem I tried the following:
a) assigning a new created wxOGLConstraint to a Python variable which does not go
out of scope
b) adding the constraint to the composite shape
c) all recalculations are fine
d) removing the constraint with either DeleteConstraint or DeleteConstraintsInvolvingChild
e) creating a new wxOGLConstraint
f) trying to assign the new constraint to the variable from step a) results in a
Windows NT error message

I have added some example code to this mail. Maybe I am using some classes the wrong way. The example fails as soon as line 86 is executed.

Does anybody know about a solution other than creating a new Python variable which does not go out of scope each time I have to change a constraint?

I need a reference in Python to the constraint, because I also want to use the SetSpacing method.

Thanks for your comments.

Joerg

Example code:

from wxPython import wx
from wxPython import ogl

ogl.wxOGLInitialize()

class MySatteliteShape(ogl.wxRectangleShape):
    def __init__(self, canvas, parent):
        ogl.wxRectangleShape.__init__(self, 10, 10)
        self.constraint = None
        self.SetCanvas(canvas)
        self.SetFixedSize(wx.true, wx.true)
        parent.AddChild(self)
        self.Show(wx.true)
        
class MyCompositeShape(ogl.wxCompositeShape):
    def __init__(self, canvas):
        ogl.wxCompositeShape.__init__(self)
        self.SetCanvas(canvas)
        self.SetDraggable(wx.true)
        self.SetX(100)
        self.SetY(100)
        self.icon = ogl.wxRectangleShape(50, 50)
        self.icon.SetCanvas(canvas)
        self.icon.SetDraggable(wx.true)
        self.icon.SetFixedSize(wx.true, wx.true)
        self.icon.Show(wx.true)
        self.AddChild(self.icon)
        self.Recompute()
        self.CalculateSize()
        self.Show(wx.true)
        
class MyCanvas(ogl.wxShapeCanvas):
    def __init__(self, parent):
        print 'Initializing base class canvas'
        ogl.wxShapeCanvas.__init__(self, parent)
        print 'Setting scrollbars'
        self.SetScrollbars(2, 2, 150, 150)
        print 'Setting background colour'
        self.SetBackgroundColour(wx.wxWHITE)
        print 'Creating diagram'
        self.diagram = ogl.wxDiagram()
        print 'Setting diagram in canvas'
        self.SetDiagram(self.diagram)
        print 'Setting canvas in diagram'
        self.diagram.SetCanvas(self)
        print 'Creating composite shape'
        self.shape = MyCompositeShape(self)
        print 'Adding shape to diagram'
        self.diagram.AddShape(self.shape)
        print 'Redrawing'
        dc = wx.wxClientDC(self)
        self.PrepareDC(dc)
        self.shape.Move(dc, 100, 100)
        self.diagram.Clear(dc)
        self.diagram.Redraw(dc)
        print 'Creating sattelite shape'
        self.sattelite = MySatteliteShape(self, self.shape)
        print 'Creating constraint'
        self.sattelite.constraint = ogl.wxOGLConstraint(ogl.gyCONSTRAINT_ABOVE, self.shape.icon, [self.sattelite])
        print 'Adding constraint'
        self.shape.AddConstraint(self.sattelite.constraint)
        print 'Recomputing composite constraints'
        self.shape.Recompute()
        print 'Finished MyCanvas.__init__'
        
    def OnLeftClick(self, x, y, keys=0):
        print 'Redrawing'
        dc = wx.wxClientDC(self)
        self.PrepareDC(dc)
        print 'Recomputing composite constraints'
        self.shape.Recompute()
        self.diagram.Clear(dc)
        self.diagram.Redraw(dc)
        print 'Finishe on left click'
        
    def OnRightClick(self, x, y, keys=0):
        print 'Deleting constraints'
        self.shape.DeleteConstraintsInvolvingChild(self.sattelite)
        print 'Creating new constraint'
        constraint = ogl.wxOGLConstraint(ogl.gyCONSTRAINT_BELOW, self.shape.icon, [self.sattelite])
        print 'Adding new constraint'
        self.shape.AddConstraint(constraint)
        print 'Recomputing composite constraints'
        self.shape.Recompute()
        print 'Storing new constraint'
        self.sattelite.constraint = constraint
        print 'Finished on right click'
        
class MyFrame(wx.wxFrame):
    def __init__(self):
        print 'Initializing base class frame'
        wx.wxFrame.__init__(self, None, -1, 'Test of wxOGLConstraints')
        print 'Creating canvas'
        self.canvas = MyCanvas(self)
        print 'Finished MyFrame.__init__'
        
    def OnCloseWindow(self, event):
        self.Destroy()

class MyApp(wx.wxApp):
    def OnInit(self):
        frame = MyFrame()
        frame.Show(wx.true)
        self.SetTopWindow(frame)
        print 'Finished MyApp.OnInit'
        return wx.true
        
app = MyApp(0)
app.MainLoop()

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users