display.remove won't remove display group

I have a module which creates a popup window.  It has a close button which calls display.remove on the display group.  But for some reason, the display group is still there… Code is posted below.  Can’t figure out what’s wrong…

local M = {} local dg function M.create() dg = display.newGroup() dg.x, dg.y = display.contentCenterX, display.contentCenterY local function closePopup(event) if(event.phase == "began") then elseif(event.phase == "ended") then print("Close popup!") display.remove(dg) dg = nil end return true end local backdrop = display.newImageRect(dg, "sprites/you\_won\_backdrop.png", 700,1000) local close\_button = display.newImageRect(dg, "sprites/red\_arrow.png", 25,25) close\_button:addEventListener("touch", closePopup) end return M

As long as you’ve got those image paths and names set up properly, then that’ll do it. I don’t see any errors there.

However, I would expect you to want to actually return that “dg” to whatever file it is called from, i.e. remove “local dg” from line 3 and add “local” in front of dg on line 6.

Now, the question is, why do you think that the display group is still there even after you’ve run “closePopup”? Are you accidentally running the create function multiple times? If you are, then because you’ve got “local dg” outside of the function, then each additional popup will overwrite the previous one.

That was it!  I added a print statement to the top of the function and sure enough, it was accidentally being created multiple times from the other module. 

Thanks Xedur@Spyric    for thinking of that!