title: Shifting display groups around in a lua table
body: <p>I am working on a camera module for my game.  I am specifically designing it to work with very large amounts of randomly generated data.  The data are in the form of 2d chunks.  They are labeled and referenced by actual x, y coordinates.  Each chunk has a group attached to it in order to contain the images associated with it.  I have a group that contains only the chunks that are on screen ( my simple simple attempt at culling ).  When the player moves in any direction by the width of a chunk, all the current chunks need to shift in the table by one element and a new row ( or column ) of chunks needs to be loaded in the edge row ( or column ).  My problem comes when I do the shifting.  I am getting an error that says that I have either already removed that image group or its parent group.  The problem is that I haven't done this.  The following code is the section I am having issues in ( I think the whole code is to large to post all at once ):</p>
  
  <p>Note:  At the VERY end is the section where the removal of elements occurs.  I have it marked with asterisks.</p>
  
  <pre><code>if( changeBottom == true ) then
   for i = 1, newView.numViewChunksY - 1 do
    for j = 1, newView.numViewChunksX do
     newView.imageChunkTable[i][j] = newView.imageChunkTable[i + 1][j]
    end
   end
   for j = 1, newView.numViewChunksX do
   local i = newView.numViewChunksY
   local metaX
   local metaY
   if( newView.metaLoadingData.L == true ) then
    if( ( newView.curChunk.x + ( j - 3 )*chunkSize ) &lt; newView.curMetaChunk.x ) then
     metaX = 1
    else
     metaX = 2
    end
   elseif( newView.metaLoadingData.R == true ) then
    if( ( newView.curChunk.x + ( j - 3 )*chunkSize ) &gt; ( newView.curMetaChunk.x + metaChunkSize*chunkSize ) ) then
     metaX = 2
    else
     metaX = 1
    end
   else
    metaX = 1
   end
   if( newView.metaLoadingData.T == true ) then
    if( ( ( newView.curChunk.y + ( i - 2 )*chunkSize ) &lt; newView.curMetaChunk.y ) ) then
     metaY = 1
    else
     metaY = 2
    end
   elseif( newView.metaLoadingData.B == true ) then
    if( ( newView.curChunk.y + ( i - 2 )*chunkSize ) &gt;= newView.curMetaChunk.y + metaChunkSize*chunkSize ) then
     metaY = 2
    else
     metaY = 1
    end
   else
    metaY = 1
   end
   local chunk = newView.metaChunkTable[metaY][metaX].chunkTable[ ( newView.curChunk.y + ( i - 2 )*chunkSize )/chunkSize - newView.metaChunkTable[metaY][metaX].y/chunkSize + 1 ][ ( newView.curChunk.x + ( j - 3 )*chunkSize )/chunkSize - newView.metaChunkTable[metaY][metaX].x/chunkSize + 1 ]
   newView.imageChunkTable[newView.numViewChunksY][j].displayGroup:removeSelf()----------
   newView.imageChunkTable[newView.numViewChunksY][j] = nil                       *
   newView.imageChunkTable[newView.numViewChunksY][j] = imageChunk.new( chunk )   *
  end
  </code></pre>
  
  <p><strong>EDIT :</strong>  The exact wording of the error I am getting ( without file paths ) is as follows:</p>
  
  <p>ERROR: Attempt to remove an object that's already been removed from the stage or whose parent/ancestor group has already been removed.</p>
  
  <p>Here is the line that is referenced by the debugger:</p>
  
  <pre><code>newView.imageChunkTable[newView.numViewChunksY][j].displayGroup:removeSelf()
  </code></pre>
  
  <p>I have marked it in the code with hyphens.</p>
  
  <p><strong>EDIT 2 :</strong>  imageChunk module:</p>
  
  <pre><code>local imageChunk = {}
  local imageChunk_mt = { __index = imageChunk }
  
  function imageChunk.new( infoChunk )
   local chunkSize = 10
   local tileSize = 48
  
   local newImageChunk = {
    objTable = {},
    displayGroup = display.newGroup()
          }
   for i = 1, infoChunk.numLayers do
    newImageChunk.objTable[i] = {}
    for j = 1, infoChunk.layer[i].numMembers do
     newImageChunk.objTable[i][j] = display.newImage( infoChunk.layer[i].group[j].kind.."/"..infoChunk.layer[i].group[j].subKind..".png", infoChunk.layer[i].group[j].x, infoChunk.layer[i].group[j].y )
     newImageChunk.displayGroup:insert( newImageChunk.objTable[i][j] )
    end
   end
  
   return setmetatable( newImageChunk, imageChunk_mt )
  end
  
  return imageChunk
  </code></pre>