Second call loses reference

I have created a wrapper for the scrollview widget and when I first call it everything is fine - the internal ‘scrollview’ variable is referenced as intended.

When I remove the scrollview and, later, create it again, the internal ‘scrollview’ variable is still referencing the old value.

Why is this happening? Have I forgotten something fundamental about Lua closures?

local widget = require("widget") function widget.newDragItemsScrollView( params ) local scrollview = widget.newScrollView( params ) print("scrollview: ",scrollview) function scrollview:add( item, listener, dragtime, angle, radius, touchthreshold ) scrollview:insert( item ) touch = function( event ) touchevent = event if (event.phase == "began") then display.currentStage:setFocus( event.target, event.id ) event.target.hasFocus = true return true elseif (event.target.hasFocus) then if (event.phase == "moved") then scrollview:takeFocus(event) print(scrollview) -- always prints the FIRST value of scrollview return false else display.currentStage:setFocus( event.target, nil ) event.target.hasFocus = nil end return true end return false end item:addEventListener( "touch", touch ) end return scrollview end

It turns out that the event listener was not being removed - so I added a finalize() and remove the touch listener:

I have created a wrapper for the scrollview widget and when I first call it everything is fine - the internal 'scrollview' variable is referenced as intended. When I remove the scrollview and, later, create it again, the internal 'scrollview' variable is still referencing the old value. Why is this happening? Have I forgotten something fundamental about Lua closures? local widget = require("widget") function widget.newDragItemsScrollView( params ) local scrollview = widget.newScrollView( params ) print("scrollview: ",scrollview) function scrollview:add( item, listener, dragtime, angle, radius, touchthreshold ) scrollview:insert( item ) touch = function( event ) touchevent = event if (event.phase == "began") then display.currentStage:setFocus( event.target, event.id ) event.target.hasFocus = true return true elseif (event.target.hasFocus) then if (event.phase == "moved") then scrollview:takeFocus(event) print(scrollview) -- always prints the FIRST value of scrollview return false else display.currentStage:setFocus( event.target, nil ) event.target.hasFocus = nil end return true end return false end item:addEventListener( "touch", touch ) scrollview:addEventListener( "finalize", function() item:removeEventListener( "touch", touch ) end ) end return scrollview end

It turns out that the event listener was not being removed - so I added a finalize() and remove the touch listener:

I have created a wrapper for the scrollview widget and when I first call it everything is fine - the internal 'scrollview' variable is referenced as intended. When I remove the scrollview and, later, create it again, the internal 'scrollview' variable is still referencing the old value. Why is this happening? Have I forgotten something fundamental about Lua closures? local widget = require("widget") function widget.newDragItemsScrollView( params ) local scrollview = widget.newScrollView( params ) print("scrollview: ",scrollview) function scrollview:add( item, listener, dragtime, angle, radius, touchthreshold ) scrollview:insert( item ) touch = function( event ) touchevent = event if (event.phase == "began") then display.currentStage:setFocus( event.target, event.id ) event.target.hasFocus = true return true elseif (event.target.hasFocus) then if (event.phase == "moved") then scrollview:takeFocus(event) print(scrollview) -- always prints the FIRST value of scrollview return false else display.currentStage:setFocus( event.target, nil ) event.target.hasFocus = nil end return true end return false end item:addEventListener( "touch", touch ) scrollview:addEventListener( "finalize", function() item:removeEventListener( "touch", touch ) end ) end return scrollview end