title: Ghost instances popping up in vectors
body: <p>This one has me stumped.  I have three classes - a large class called <code>Level</code> that contains, among other things, a multidimensional vector of pointers to <code>Plant</code> instances and a multidimensional vector of pointers to <code>Mob</code> instances; both vectors are meant to classify the mobs and plants according to their position, so that I can search for each in a smaller vector based on their approximate location, rather than cycling through all existing plants/mobs in order to find the one nearest to a given point.</p>
  
  <p>The vectors are as follows, with the smallest std::vectors representing a square area of 128 pixels on each side.  Mobs and plants are classed by dividing their X and Y coords by 128 and adding them to the proper sector (I am careful that the resulting values are in fact integers).</p>
  
  <p><code>std::vector&lt; std::vector&lt; std::vector&lt;Plant*&gt; &gt; &gt;* m_PlantSectors</code></p>
  
  <p><code>std::vector&lt; std::vector&lt; std::vector&lt;Mob*&gt; &gt; &gt;* m_AnimalSectors</code></p>
  
  <p>The mobs sometimes need to find plants.  Here is where the problem arises: when the mobs query the multidimensional vector, searching for plants in their approximate area (if the mob's coords/128 are, say, [1,2] it searches m_PlantSectors[2][1]), they sometimes find plants that don't exist.  </p>
  
  <p>Not only that, but these plants have impossible positions, on the order of 1.9777e+33 or 3.75853e-39 (for instance).  When I try to change the color of the chosen plant to red in order to find it visually, I find that none of the plants on the screen (the only plants are ones I've hand-placed) have changed color.  </p>
  
  <p>I've marked all the plants with an integer ID; there are 36 plants, with IDs from 1-36, but the plants my mobs find have IDs like 63 or 429 - ones that can't possibly exist, since no such number of plants was created (there is a single plant creation function that consistently reports how many plants exist, so no plants are being accidentally created).  The mobs all run off to the upper-left part of the screen after the imaginary plants, and die of starvation.</p>
  
  <p>So somehow, I an creating ghost plants.  So far, I've tried two separate approaches for allowing <code>Mob</code> instances to look for <code>Plant</code> instances.  The first looks like this:</p>
  
  <pre><code>    float TargetDist = 256 * 256;
      Plant* Candidate = 0;
      Plant* ForageTarget = 0;
      int xSect = m_X / 128;
      int ySect = m_Y / 128;
      std::vector&lt;Plant*&gt; ThisSect = pLevel-&gt;CheckPSector(xSect, ySect);
      for (int i = 0; i &lt; ThisSect.size(); ++i)
      {
          cout &lt;&lt; "Searching in Sector (" &lt;&lt; ySect &lt;&lt; ", " &lt;&lt; xSect &lt;&lt; ")\n";
          Candidate = ThisSect[i];
          cout &lt;&lt; "Candidate at: " &lt;&lt; Candidate-&gt;GetX() &lt;&lt; ", " &lt;&lt; Candidate-&gt;GetY() &lt;&lt; "\n";
          Candidate-&gt;Mark();
          //Calculate distance
          float xDist = Candidate-&gt;GetX() - m_X;
          float yDist = Candidate-&gt;GetY() - m_Y;
          float tDist = sqrt(xDist * xDist + yDist * yDist);
          if (tDist &lt;= TargetDist)
          {
              ForageTarget = Candidate;
              TargetDist = tDist;
          }
      }
  </code></pre>
  
  <p>Where CheckPSector() looks like this:</p>
  
  <pre><code>std::vector&lt;Plant*&gt; Level::CheckPSector(int x, int y)
  {
      return m_PlantSectors[y][x];
  }
  </code></pre>
  
  <p>The second thing I tried was this:</p>
  
  <pre><code>        float TargetDist = 256 * 256;
          Plant* Candidate = 0;
          Plant* ForageTarget = 0;
          int xSect = m_X / 128;
          int ySect = m_Y / 128;
          std::vector&lt; std::vector&lt; std::vector&lt;Plant*&gt; &gt; &gt;* Sectors = pLevel-&gt;AccessPlantSectors();
          for (int i = 0; i &lt; (*Sectors)[ySect][xSect].size(); ++i)
          {
              cout &lt;&lt; "Searching in Sector (" &lt;&lt; ySect &lt;&lt; ", " &lt;&lt; xSect &lt;&lt; ")\n";
              Candidate = (*Sectors)[ySect][xSect][i];
              cout &lt;&lt; "Candidate at: " &lt;&lt; Candidate-&gt;GetX() &lt;&lt; ", " &lt;&lt; Candidate-&gt;GetY() &lt;&lt; "\n";
              Candidate-&gt;Mark();
              //Calculate distance
              float xDist = Candidate-&gt;GetX() - m_X;
              float yDist = Candidate-&gt;GetY() - m_Y;
              float tDist = sqrt(xDist * xDist + yDist * yDist);
              if (tDist &lt;= TargetDist)
              {
                  ForageTarget = Candidate;
                  TargetDist = tDist;
              }
          }
  </code></pre>
  
  <p>Using this:</p>
  
  <pre><code>std::vector&lt; std::vector&lt; std::vector&lt;Plant*&gt; &gt; &gt;* Level::AccessPlantSectors()
  {
      return &amp;m_PlantSectors;
  }
  </code></pre>
  
  <p>Both of these, however, result in the animals finding imaginary plants and running off into the void.</p>
  
  <p>I don't want to copy potentially large multidimensional vectors into <code>Mob</code> instances on a regular basis, because there will be many such instances at any one time, and I'd like the program to run somewhat smoothly.  Even so, I just tried to do the whole thing by copying the entire vector instead of just the relevant one, and I get the same result: imaginary plants.</p>
  
  <p>I've never quite had a problem like this before; what could be happening here?</p>
  
  <p>EDIT: Perhaps I should mention that getting the chosen plants to self-report their own position and ID fails equally, turning up absurd results, so it isn't just the functions in <code>Plant</code> I use to access private members.  Meanwhile, querying all existing plants does not reveal any with the information the ghost plants self-report.</p>