On Sat, Apr 25, 2009 at 1:11 PM, lostgallifreyan <lostgallifre...@gmail.com> wrote: > I'd been creating and deleting pens and colours each time round a loop to > avoid accumulations, and returned to this issue while seeking ways to speed > up my program. > > My current attempt is this (with irrelevance stripped out): > > local DC,P=wx.wxMemoryDC(),wx.wxPen() P:SetWidth(5) P:SetStyle(wx.wxSOLID) > DC:SelectObject(BITMAP) DC:SetPen(P) DC:SetBrush(wx.wxTRANSPARENT_BRUSH) > for N=0,255,5 do > P:SetColour(N,N,N) DC:SetPen(P) DC:DrawLine(N,0,N,100) > end > DC:SelectObject(wx.wxNullBitmap) P:delete() > DC:delete() > > It works, but I'm not sure that it's safe. is P:SetColour() changing an > implied colour 'object' that would better be made explicit and assigned to a > vartiable so I can delete it along with the pen after the loop ends? The > wx.chm manual doesn't make this clear, it seems to imply that this method > avoids the need for an actual colour 'object' entirely. >
The code above is fine since the C++ wxPen::SetColour() code simply calls wxColour::Set(r,g,b) on the internal wxColour member of the wxPen. More generally; in wxLua you don't have to worry about deleting any objects that are not explicitly created by you in wxLua or returned by another function. We will trust that there are no memory leaks in wxWidgets itself, which is a safe bet. You can print out all the objects in wxLua including ones that are installed at startup: for k,v in pairs(wxlua.GetTrackedObjectInfo()) do print(k, v) end and also only the objects that you have created that need to be or will be deleted when they go out of scope and the garbage collector runs: for k,v in pairs(wxlua.GetGCUserdataInfo()) do print(k, v) end ======================== example in wxLuaEdit in the console in the bottom: p = wx.wxPen(wx.wxColour(1,2,3), 1, 1) for k,v in pairs(wxlua.GetGCUserdataInfo()) do print(k, v) end 1 wxColour(0x29846f0) 2 wxPen(0x2952780) collectgarbage('collect') for k,v in pairs(wxlua.GetGCUserdataInfo()) do print(k, v) end 1 wxPen(0x2952780) The most "dangerous" problem with GC in wxLua are those hidden objects like the wxColour above and there's no way to keep track of them. I can't think of any clever way that would be much better than simply calling the Lua function collectgarbage("collect"). Regards, John ------------------------------------------------------------------------------ Crystal Reports - New Free Runtime and 30 Day Trial Check out the new simplified licensign option that enables unlimited royalty-free distribution of the report engine for externally facing server and web deployment. http://p.sf.net/sfu/businessobjects _______________________________________________ wxlua-users mailing list wxlua-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wxlua-users