title: C++ using vector as static container for class instances
body: <p>I use a vector as static member of a class to count all the instance of the class itself and its derived classes. However When trying to resize the container I get a stack-overflow thrown from the vector itself.   </p>
  
  <pre><code> // initialize static values:
      auto_ptr&lt;HandleManager&gt; ID3OBJ::HM = auto_ptr&lt;HandleManager&gt;(new HandleManager());
      auto_ptr&lt;vector&lt;shared_ptr&lt;ID3OBJ&gt;&gt;&gt; ID3OBJ::ObjectList = auto_ptr&lt;vector&lt;shared_ptr&lt;ID3OBJ&gt;&gt;&gt;(new vector&lt;shared_ptr&lt;ID3OBJ&gt;&gt;{});
  </code></pre>
  
  <p>I initialize the static member as empty as shown above.</p>
  
  <pre><code>// constructors:
          ID3OBJ::ID3OBJ(double x, double y, double z) : X(x), Y(y), Z(z), Handle(this-&gt;HM-&gt;addHandle()) { ObjectList-&gt;push_back(auto_ptr&lt;ID3OBJ&gt;(this));}
          ID3OBJ::ID3OBJ() : X(0), Y(0), Z(0), Handle(this-&gt;HM-&gt;addHandle()) { ObjectList-&gt;push_back(shared_ptr&lt;ID3OBJ&gt;(this));}
  
          Vector::Vector(double x, double y, double z) { X = x; Y = y; Z = z;
          ObjectList-&gt;push_back(auto_ptr&lt;Vector&gt;(this));}
          Vector::Vector() {
              X = 0; Y = 0; Z = 0;
              ObjectList-&gt;push_back(shared_ptr&lt;Vector&gt;(this));}
  </code></pre>
  
  <p>The constructors add any new instance to the instancelist, which is called ObjectList. This is working as intended.</p>
  
  <pre><code>// deconstructors:
      ID3OBJ::~ID3OBJ()
      {
          string tempH = this-&gt;Handle;
          auto iter = ObjectList-&gt;end();
          if (ObjectList-&gt;size() == HM-&gt;assignedHandles())
          {
              iter = remove_if(ObjectList-&gt;begin(), ObjectList-&gt;end(), [&amp;](shared_ptr&lt;ID3OBJ&gt; ptr) {return ptr-&gt;getHandle() == tempH; });
          }
          ObjectList-&gt;erase(iter, ObjectList-&gt;end());
          this-&gt;HM-&gt;removeHandle(this-&gt;Handle);
      }
  
      Vector::~Vector()
      {
          string tempH = this-&gt;Handle;
          auto iter = ObjectList-&gt;end();
          if (ObjectList-&gt;size() == HM-&gt;assignedHandles())
          {
              iter=remove_if(ObjectList-&gt;begin(), ObjectList-&gt;end(), [&amp;](shared_ptr&lt;ID3OBJ&gt; ptr) {return ptr-&gt;getHandle() == tempH; });
          }
          ObjectList-&gt;erase(iter, ObjectList-&gt;end());
      }
  </code></pre>
  
  <p>As far as I understand remove_if replaces any occurances whose pred returns true with the element after the occurance. Means if the pred with vec[3] as argument returns true, vec[2] points to vec[4] instead of vec[3].
  Hence the erase function is needed to shorten the container length, however as soon as I implement this shortening an error occurs.</p>
  
  <p>Header File:</p>
  
  <pre><code>// class-name:      ID3OBJ
      // Date:            30.01.2017
      // Version:         1.0
      // Description:     The class works as base class for all used 3D-Objects, and defines the operations all 3D-Objects have, namely the Direction in case of a vector, or origion in all other cases
      //
  
      class ID3OBJ
      {
      public:
          double X;
          double Y;
          double Z;
          static auto_ptr&lt;vector&lt;shared_ptr&lt;ID3OBJ&gt;&gt;&gt; ObjectList;
  
          ID3OBJ(double x, double y, double z);
          ID3OBJ();
          ~ID3OBJ();
          const string getHandle();
  
      protected:
          string Handle;
          static auto_ptr&lt;HandleManager&gt; HM;
      };
  
      // class-name:      I3DM
      // Date:            23.03.2017
      // Version:         1.0
      // Description:     The class works as Interface for classes which can do Vector-operations 
      //
      template &lt;class T&gt; class I3DM : public virtual ID3OBJ
      {
      public:
          using ID3OBJ::X;
          using ID3OBJ::Y;
          using ID3OBJ::Z;
  
          static auto_ptr&lt;vector&lt;shared_ptr&lt;T&gt;&gt;&gt; ObjectList;
      protected:
          using ID3OBJ::Handle;
          using ID3OBJ::HM;
      };
      // class-name:      Vector
      // Date:            30.01.2017
      // Version:         1.0
      // Description:     The class works as vector, it provides an interface to acces and modify vectors, aswell as most of the vector operations
      //
      class Vector : public virtual I3DM&lt;Vector&gt;
      { 
      public:
          using I3DM&lt;Vector&gt;::X;
          using I3DM&lt;Vector&gt;::Y;
          using I3DM&lt;Vector&gt;::Z;
          using I3DM&lt;Vector&gt;::ObjectList;
          Vector(double x, double y, double z);
          Vector();
          ~Vector();
      //I'm not sure if the protected members have to be provided aswell in the header file
      protected:
          using ID3OBJ::Handle;
          using ID3OBJ::HM;
      };
  </code></pre>
  
  <p>HM-header:</p>
  
  <pre><code>class HandleManager
      {
      public:
          HandleManager();
          const int assignedHandles();
          const string addHandle();
          void removeHandle(string hexstring);
      protected:
          int AssignedHandles;
          forward_list&lt;int&gt; FreeHandles;
          bool FreeHandlesAvailable;
      };
  </code></pre>
  
  <p>CPP:</p>
  
  <pre><code>const int string_to_hex(string s)
  {
      int returnvalue;
      stringstream stream;
      stream &lt;&lt; hex &lt;&lt; s;
      stream &gt;&gt; returnvalue;
      return returnvalue;
  }
  HandleManager::HandleManager()
  {
      this-&gt;FreeHandlesAvailable = false;
      this-&gt;AssignedHandles = 0;
  }
  const int HandleManager::assignedHandles()
  {
      return this-&gt;AssignedHandles;
  }
  const string HandleManager::addHandle()
  {
      string returnValue;
      if (this-&gt;FreeHandlesAvailable)
      {
          returnValue = int_to_hex(this-&gt;FreeHandles.front());
          this-&gt;FreeHandles.pop_front();
          this-&gt;AssignedHandles++;
          if (this-&gt;FreeHandles.empty()) { this-&gt;FreeHandlesAvailable = false; }
      }
      else
      {
          returnValue = int_to_hex(this-&gt;AssignedHandles);
          this-&gt;AssignedHandles++;
          if (this-&gt;AssignedHandles == 1) { returnValue = int_to_hex((int)0); }
          }
      return returnValue;
  }
  void HandleManager::removeHandle(string hexstring)
  {
      this-&gt;FreeHandlesAvailable = true;
      this-&gt;FreeHandles.push_front(string_to_hex(hexstring));
      this-&gt;AssignedHandles--;
  }
  </code></pre>
  
  <p>error message:</p>
  
  <blockquote>
    <p>Unhandled exception at 0x00C01899 in RVE.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x01002F48). occurred</p>
  </blockquote>
  
  <p>The parameter 0x00000001 is most likely a handle, is there any way to search for the object which has the memory adress given? (0x01002F48)</p>