简体   繁体   中英

Removing objects created by another class in Lua / Corona SDK and storyboard

I'm trying to figure out the best way to fix a memory leak in my app and use external classes. I am coding in Corona SDK and using Storyboard. I don't think I am removing objects properly when I create them through classes. Can you take a look and help with the following

1) At the bottom of the code, I show how I remove the keyboard. Is that sufficient or do I need to do more since the keyboard was created through another file?

2) in keyboard.lua , I need to create the objects in a way that thekeyboard.lua file can manipulate them with functions later. I did this by declaring theKeyboard, theCursor, theBackground.

Is there any reason to do that vs calling them M.theKeyboard, M.theCursor, M.theBackground and not declaring them in advance since M is local?

3) Would you implement this keyboard class differently? If so, can you give me pointers?

Here is example code. I want to reuse this keyboard code across my app. There should only be one keyboard at anytime. I want to remove the keyboard completely when the user exits the scene because many screens do not require the keyboard.

-- keyboard.lua
local M = {}
local theKeyboard, theCursor, theBackground

function M.newBackground()
    if theBackground then 
       theBackground = nil
    end
    local newBackground = display.newRect(0,0,0,0)
    -- set position, size, color, etc
    theBackground = newBackground
    return newBackground
end

... many other functions to create cursor, textlabels, etc

function M.newKeyboard()
    if theKeyboard then 
       theKeyboard = nil
    end
    theKeyboard = display.newGroup()
    theCursor = M.newCursor()
    theBackground = M.newBackground()

    --  lots more stuff... like I create buttons for each key on the keyboard

    theKeyboard:insert(theCursor)
    theKeyboard:insert(theBackground)
    return theKeyboard
end

function M.removeKeyboard()
   display.remove(theCursor)
   display.remove(theBackground)
   display.remove(theKeyboard)
   theCursor = nil
   theBackground = nil
   theKeyboard = nil
end

return M

and then my app uses storyboard so here's an example of how I integrate the keyboard into a scene.

local keyboard = require ( "keyboard" )
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

local keyboardGroup, otherobjects 

function scene:createScene( event )
    local group = self.view
    keyboardGroup = keyboard.newKeyboard
    group:insert( keyboardGroup )
    end
end

-- other code


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
    local group = self.view

    --  Is this sufficient for removing the keyboard completely?
        keyboard.removeKeyboard()
        keyboardGroup = nil
    end
end


---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------
-- additional code that comes with storyboard

return scene

Please try -

if theKeyboard then 
   theKeyboard:removeSelf()
   theKeyboard = nil
end

I think it will solve your problem.

In addition to what @Varsha say, you should have this code block. In corona its not enough to just remove objects. collectgarbage() function do the main work. Whenever you type object = nil afterwards it will be deleted by garbage collector.

local monitorMem = function() -- Garbage collector

    collectgarbage()
    --print( "MemUsage: " .. collectgarbage("count") )

    local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000
    --print( "TexMem:   " .. textMem )
end
Runtime:addEventListener( "enterFrame", monitorMem )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM