From: Wojtek F. <woj...@gm...> - 2011-07-22 09:16:14
|
Hi! I'm new to VPython project but I found a memory leak. There was something mentioned about this problem before. I found it here: http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTim8RL8kjW0%3DSK%2B9f-2A5TCQv6wAu9aTpBmisbNS%40mail.gmail.com&forum_name=visualpython-users. But unfortunately I didn't find the solution. I want to use VPython to render a surface which is beeing changed during the program execution. To show you what I'm talking about I changed one of the example programs (faces_heighfield.py). Now it looks something like this: <code> from visual import * class Model: def __init__(self): self.frame = frame() self.model = faces(frame=self.frame, color=color.cyan) self.vertices = [] def FacetedTriangle(self, v1, v2, v3, color=color.white): """Add a triangle to the model""" for v in (v1,v2,v3): self.vertices.append(v) def FacetedPolygon(self, *v): """Appends a planar polygon of any number of vertices to the model""" for t in range(len(v)-2): self.FacetedTriangle( v[0], v[t+1], v[t+2] ) class Mesh (Model): def __init__(self): Model.__init__(self) def Draw(self, xvalues, yvalues, zvalues): points = zeros( xvalues.shape + (3,), float ) points[...,0] = xvalues points[...,1] = yvalues points[...,2] = zvalues self.vertices = [] for i in range(zvalues.shape[0]-1): for j in range(zvalues.shape[1]-1): self.FacetedPolygon( points[i,j], points[i,j+1], points[i+1,j+1], points[i+1,j] ) self.model.pos = self.vertices self.model.make_normals() self.model.smooth() self.model.make_twosided() x = arange(-1,1,2./20) y = arange(-1,1,2./20) z = zeros( (len(x),len(y)), float ) x,y = x[:,None]+z, y+z m = Mesh() i = 0 dt = 0.1 while True: m.Draw( x, (sin(x*pi + i*dt)+sin(y*pi + i*dt))*0.2, y ) i += 1 rate(10) </code> When running this code we see quite a nice surface movement. That is what I need. But the memory usage grows infinitely! My question is... is there any fix for this memory leak? Or do you know other way to achieve similar visual effect in VPython? I also tried to render the surface using convexes or points instead of faces but the memory leak always existed... Thanks in advance for your help. Greetings, Wojciech Frycz |
From: Bruce S. <Bru...@nc...> - 2011-07-23 22:57:58
|
The discussion you found about a memory leak referred to a bug that was fixed last March. From the "recent developments" section of vpython.org: 2011-03-01 Visual 5.51: Fix memory leak on Windows and Mac. You've found a different memory leak, one that has not previously been reported. The leak that was fixed was a bug in rendering the scene on Windows and Mac, with the effect that setting object.visible = False eliminated the leak. Setting object.visible = False in your program doesn't make any difference: the leak is still there. Thanks much for reporting this. At the moment I don't know what is causing the leak. I note that commenting out make_normals, smooth, and make_twosided has no effect on the leak. The leak is associated with self.model.pos = self.vertices (but, I repeat, independent of whether the faces object is visible or not). I also note that if one executes m.Draw just once (putting a break at the end of the while loop), there is no leak, confirming that the leak is not associated with the repeated rendering that is automatically performed, unlike the case with the old leak. Since the problem is somehow associated with the statement self.model.pos = self.vertices, I made a version of your program that allocates once and for all the pos array, then assigns values to the pos array rather than constructing a vertices array first. The following program does not show a memory leak. This of course leaves open the question of why there is a leak when one assigns an array to faces.pos. from visual import * class Model: def __init__(self): self.frame = frame() self.model = faces(frame=self.frame, color=color.cyan, visible=True) class Mesh (Model): def __init__(self, z): Model.__init__(self) self.length = (z.shape[0]-1)*(z.shape[1]-1)*6 # length of one-sided data self.model.pos = zeros( (2*self.length,3), float ) # space for two-sided data def Update(self, xvalues, yvalues, zvalues): points = zeros( xvalues.shape + (3,), float ) points[...,0] = xvalues points[...,1] = yvalues points[...,2] = zvalues p = 0 for i in range(zvalues.shape[0]-1): for j in range(zvalues.shape[1]-1): self.model.pos[p ] = points[i,j] self.model.pos[p+1] = points[i,j+1] self.model.pos[p+2] = points[i+1,j+1] self.model.pos[p+3] = points[i,j] self.model.pos[p+4] = points[i+1,j+1] self.model.pos[p+5] = points[i+1,j] self.model.pos[self.length+p ] = points[i,j] self.model.pos[self.length+p+2] = points[i,j+1] self.model.pos[self.length+p+1] = points[i+1,j+1] self.model.pos[self.length+p+3] = points[i,j] self.model.pos[self.length+p+5] = points[i+1,j+1] self.model.pos[self.length+p+4] = points[i+1,j] p += 6 self.model.make_normals() self.model.smooth() x = arange(-1,1,2./20) y = arange(-1,1,2./20) z = zeros( (len(x),len(y)), float ) x,y = x[:,None]+z, y+z m = Mesh(y) i = 0 dt = 0.05 while True: m.Update( x, (sin(x*pi + i*dt)+sin(y*pi + i*dt))*0.2, y ) i += 1 rate(100) Bruce Sherwood On Fri, Jul 22, 2011 at 3:15 AM, Wojtek Frycz <woj...@gm...> wrote: > Hi! > I'm new to VPython project but I found a memory leak. There was something > mentioned about this problem before. I found it here: > http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTim8RL8kjW0%3DSK%2B9f-2A5TCQv6wAu9aTpBmisbNS%40mail.gmail.com&forum_name=visualpython-users. > But unfortunately I didn't find the solution. |