Google

main
dspblog
cyclic_signals
FFT_interpolation
FFT_interpolation_how_does_it_work
FFT_smoothness_cyclic
discrete_time_reconstruction
discrete_time_reconstruction2
Nyquist_on_the_edge
FFT_delay_special_case
Fourier_reconstruction
pulse_and_Nyquist
FFT_complexError
matlabOctave
matlab_upsampling
matlab_downsampling
FFT_delay
FFT_filter_example
FFT_interpolation_example
FFT_bin_frequencies
fit_signal
FFT_peaksearch_audio_example
matlab_binary_readwrite
Octavesvg
C
FFTW_example
looprecord
SNR
SNR3
SNR_example_96kAudio
SNR_FFT_correlation_example
lua
luagpib
luasplit
luadump
mnoofltk
wxLuaDll
wxLua_loadAsDll
wxLua_HelloWorld
wxLua_simpleButton
wxLua_resourceManagement
wxLua_XMLparser
DSP
IQ_LO
IQ_LO_2
optimum_receiver
DSP_basics
sampleRateChange_terms
dirac_pulse
freqresp_s
zfilter_example
freqresp_z
freqresp_z_sign
misc
zero_forcing_equalizer_example
nonminphase_inverse
periodic_spectrum
lagrange_multipliers
Entropy
RC_chopper
TRex450_setup
EP100
EP100SE
EP100Gremlins
essential_spares
tail_rotor
motoradjustment
blade_balancing
blade_repair
GAUI_SAE12A
Walkera43


valid html (click to verify)



prevupnextdisable ads

wxLua: delete() or not?

Summary

wxLua objects must be deallocated using object:delete() once they are no longer needed.
Otherwise the application will allocate more and more system resources.

Introduction

Many 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.

Application

The 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.

Observation

Memory 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

Code

Download
-- *****************************************************
-- * 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() 


prevupnextdisable ads

© 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