title: Working around memory issues caused by global static variables
body: <p>Caveat: This question arose because I have to work with a huge pile of awful code, without proper documentation, written by someone else as a research project 6 years ago. Obviously a better solution is <em>not</em> to cause these issues in the first place, with proper design...</p>
  
  <p>That said the question is: What is the best way to get out of a situation like this:</p>
  
  <ol>
  <li>A class allocates memory on the heap, and frees it in the destructor.</li>
  <li>Somewhere, an instance of the class is declared at global scope.</li>
  <li>A function exists that initializes this instance.</li>
  <li>That function's return value is used to initialize a static variable.</li>
  <li>The globally scoped variable is used outside of the static scoping.</li>
  </ol>
  
  <p>Minimal working example:</p>
  
  <p>File "myclass.h":</p>
  
  <pre><code>#ifndef MYCLASS_H
  #define MYCLASS_H
  
  #include&lt;vector&gt;
  using namespace std;
  
  class myclass{
          vector&lt;int&gt; *onTheHeap;
  public:
          myclass(int len=0){
                  onTheHeap = new vector&lt;int&gt;(len);
          }
  
          ~myclass(){
                  delete onTheHeap;
          }
  
  };
  
  #endif
  </code></pre>
  
  <p>file "static_loader.cpp"</p>
  
  <pre><code>#include "class.h"
  myclass existsForever;
  
  int cause_static_global_creation(){
          existsForever = myclass(5);
  
  }
  
  static int bootstrap = cause_static_global_creation();
  </code></pre>
  
  <p>and file "main.cpp":</p>
  
  <pre><code>#include "class.h"
  
  extern myclass existsForever;
  
  int main(){
          return 0;
  }
  </code></pre>
  
  <p>Build with:</p>
  
  <pre><code>g++ -g -c static_loader.cpp
  g++ -g main.cpp static_loader.o
  </code></pre>
  
  <p>and run as:</p>
  
  <pre><code> valgrind --leak-check=full ./a.out
  </code></pre>
  
  <p>Result: The variable is freed when its destructor is called in the exit handler's below main, but <em>also</em> in the static_initialization_and_destruction_0 function below main from static_loader!</p>
  
  <p>Is there a way to ensure that these variables are freed <em>exactly</em> once that doesn't involve re factoring the code extensively? In the library I have to work with, there are several dozen instances of this pattern...</p>
  
  <p>EDIT:</p>
  
  <p>Adding functions:</p>
  
  <pre><code>    void operator=(myclass other){
              delete this-&gt;onTheHeap;
              this-&gt;onTheHeap = other.onTheHeap;
      }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>    myclass(const myclass&amp; other){
              this-&gt;onTheHeap = new vector&lt;int&gt;(*(other.onTheHeap));
      }
  </code></pre>
  
  <p>Does not change the behavior.</p>
  
  <p>Second EDIT:</p>
  
  <pre><code>    myclass&amp; operator=(const myclass&amp; other){
              delete this-&gt;onTheHeap;
              this-&gt;onTheHeap = new vector&lt;int&gt;(*(other.onTheHeap));
              return *this;
      }
  </code></pre>
  
  <p>Resolves all the issues. My library has a memory leak with sources like this anyway, but I'm no longer sure how to reproduce it. At least it's not this though, and thanks for the suggestions on refactoring etc. as well!</p>