bad argument #-2 to 'insert' (Proxy expected, got nil)

Hi,

I have a MainMenu containing a Play button and leading to my Game. In my Game scene, I have a “Back” button returning to the MainMenu. 

If I try to “Play” again after going back to the main menu, the app crashes with the following:

ERROR: Runtime error bad argument #-2 to 'insert' (Proxy expected, got nil) stack traceback: [C]: in function 'insert' ?: in function 'gotoScene' C:\Users\...\scenes\mainMenu.lua:26: in function '\_onRelease' ?: in function '?' ?: in function '?' ?: in function \<?:190\>

I’ve read several posts from other users but I cannot get to know which call/line causes the issue.

My MainMenu looks like:

local function onPlayButtonRelease(event) composer.gotoScene( "scenes.game") return true -- indicates successful touch end

Here is an extract of my Game file:

function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Load and show the toolbars toolbar.linitializeBottomToolbar() actionbar.initializeActionbar() infobar.initializeTopInfobar() singlebuildingbar.initializeSingleBuildingBar() --sceneGroup:insert( globalData.actionBarGroup ) --sceneGroup:insert( globalData.toolbarGroup) --sceneGroup:insert( globalData.singleBuildingBarGroup) --sceneGroup:insert( globalData.infoBarGroup) --sceneGroup:insert( globalData.resourceBarGroup) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then sceneGroup:removeSelf() -- manually remove backHomeButton sceneGroup = nil globalData.actionBarGroup:removeSelf() -- manually remove globalData.actionBarGroup globalData.actionBarGroup = nil globalData.toolbarGroup:removeSelf() globalData.toolbarGroup = nil globalData.singleBuildingBarGroup:removeSelf() globalData.singleBuildingBarGroup = nil globalData.infoBarGroup:removeSelf() globalData.infoBarGroup = nil globalData.resourceBarGroup:removeSelf() globalData.resourceBarGroup = nil elseif ( phase == "did" ) then end end

And an example of my actionbar/toolbar/topbar being initialized:

local M = {} local globalData = require( "scripts.globalData" )-- Load pseudo global variable to make them accessible from a module to another local widget = require("widget") function M.initializeActionbar() globalData.actionBarGroup = display.newGroup() -- Creates a new display group for the action bar icons globalData.actionBarGroup.alpha = 1 -- Displays the action bar -- Creates the bacHomeButton button globalData.backHomeButton = widget.newButton( { width = 150, height = 150, defaultFile = "images/home.png" } ) globalData.backHomeButton.x = 75 globalData.backHomeButton.y = 320 globalData.actionBarGroup:insert( globalData.backHomeButton ) -- Inserts button into actionbar group end -- initializeActionbar return M

If I uncomment AT LEAST one  sceneGroup:insert in the scene:show , the error disapears, but then I have other problems (my Qiso map appears on top of every Composer element, even if it is added to the scene before the different elements).

When you leave the scene you seem to be deleting sceneGroup and setting it to nil. This isn’t something you should do yourself, let composer handle that. Unless you are asking composer to removeScene, the scene stays in memory for the next time it’s called. So you’re deleting sceneGroup, when the scene gets requested the second time sceneGroup no longer exists.

You also seem to be removing everything manually. If you want a clean slate each time you go to a scene, in each scene’s show function, under the ‘did’ phase, run this code:

[lua]

local previous = composer.getSceneName(“previous”)

    if previous ~= “main” and previous then

      composer.removeScene(previous, false)

    end

[/lua]

This will delete the previous scene from memory. Then everything that was inserted into sceneGroup will be removed automatically.

I removed the sceneGroup deletion and setting it to nil and I no longer get the error.

When you leave the scene you seem to be deleting sceneGroup and setting it to nil. This isn’t something you should do yourself, let composer handle that. Unless you are asking composer to removeScene, the scene stays in memory for the next time it’s called. So you’re deleting sceneGroup, when the scene gets requested the second time sceneGroup no longer exists.

You also seem to be removing everything manually. If you want a clean slate each time you go to a scene, in each scene’s show function, under the ‘did’ phase, run this code:

[lua]

local previous = composer.getSceneName(“previous”)

    if previous ~= “main” and previous then

      composer.removeScene(previous, false)

    end

[/lua]

This will delete the previous scene from memory. Then everything that was inserted into sceneGroup will be removed automatically.

I removed the sceneGroup deletion and setting it to nil and I no longer get the error.