|
|
wxLua: delete() or not?Summary
IntroductionMany objects are created, while a wxLua application is running.Objects consume system resources - memory, and possibly even operating system handles - and need to be returned after use with the object:delete() method. ApplicationThe provided example application creates a million wxColour() objects, when a button is clicked.One button will immediately delete the object, the other button does not. ObservationMemory consumption can be observed in Windows using the taskmanager.As long as objects are deleted, it remains flat. Occasionally it may increases (in particular, if more memory has been allocated earlier) but the memory can be recovered through the garbage collector. But the objects that are not deleted occupy the memory “forever”. The garbage collector cannot free them. Creating and not delete()ing objects wastes memory in wxLua and should be avoided. This behaviour is neither a memory leak nor a bug. Code CodeDownload-- *****************************************************
-- * Purpose:
-- * wxLua example that shows what happens, if one does not
-- * delete resources
-- *****************************************************
-- (optional) explicitly load wxLua libraries
local p=print
if not wx then require("wx") end
-- The "frame" is the main window
local my_frame = wx.wxFrame(wx.NULL, -- no parent
wx.wxID_ANY, -- don't assign ID
"wxLua example", -- title
wx.wxDefaultPosition,
wx.wxSize(320, 110) -- initial size of the window
)
local ID_my_button1=10 -- an arbitrary number that from now on identifies the button
local ID_my_button2=11 -- same
local ID_my_button3=12 -- same
-- create panel object
local my_panel = wx.wxPanel(my_frame, wx.wxID_ANY)
-- create buttons inside of panel
local my_button1=wx.wxButton(my_panel, -- parent
ID_my_button1, -- identification number
"Create and delete 1000000 colors", -- the button label
wx.wxPoint(10, 10), -- the position (top left corner)
wx.wxSize(300, 20) -- width and height
)
local my_button2=wx.wxButton(my_panel,
ID_my_button2,
"Create and do NOT delete 1000000 colors",
wx.wxPoint(10, 30),
wx.wxSize(300, 20)
)
local my_button3=wx.wxButton(my_panel,
ID_my_button3,
"collectgarbage()",
wx.wxPoint(10, 50),
wx.wxSize(300, 20)
)
-- Callback function, will be bound to clicking the button
local my_callback=
function (event)
local ID=event:GetId()
local c
if ID==ID_my_button1 then
-- create a million colors and delete them immediately
for i2=1, 1000000 do
c=wx.wxColour(math.random(255), math.random(255), math.random(255))
c:delete()
end
elseif ID==ID_my_button2 then
-- create a million colors without deleting them
for i=1, 1000000 do
c=wx.wxColour(math.random(255), math.random(255), math.random(255))
end
else
assert(nil)
end
end
-- Bind the callback functions to the buttons
my_panel:Connect(ID_my_button1,
wx.wxEVT_COMMAND_BUTTON_CLICKED,
my_callback
)
my_panel:Connect(ID_my_button2,
wx.wxEVT_COMMAND_BUTTON_CLICKED,
my_callback
)
my_panel:Connect(ID_my_button3,
wx.wxEVT_COMMAND_BUTTON_CLICKED,
function() collectgarbage("collect") end
)
-- Show the window
my_frame:Show(true)
-- (optional) explicitly hand over control to wxLua
wx.wxGetApp():MainLoop()
© Markus Nentwig 2007-2008 The content of this page is provided without any warranty and may not be reproduced without permission. Comments? Questions?Please send me a mail! mnentwig@elisanet.fi |