title: explicitly call managed destructor from unmanaged c++
body: <p>I have a native C++ project that makes use of .NET charting utilities through a wrapper class. A cut-down version of the wrapper class is something like this;</p>
  
  <pre><code>class ChartWrapper
  {
  private:
      gcroot&lt;ChartNamespace::ManagedChartClass^&gt;* m_Chart;
  
  public:
      ChartWrapper(): m_Chart(new gcroot&lt;ChartNamespace::ManagedChartClass^&gt;)
      {
          *m_Chart = gcnew ChartNamespace::ManagedChartClass;
      }
  
      ~ChartWrapper()
      {
          delete m_Chart;
      }
  
      // Methods to interact with the chart
  }
  </code></pre>
  
  <p>A function will take care of instantiating, manipulating, and deleting the chart through the wrapper;</p>
  
  <pre><code>void CreateChart()
  {
      ChartWrapper* chart = new ChartWrapper();
      // Do stuff to the chart
      delete chart;
  }
  </code></pre>
  
  <p>There is the potential of creating hundreds of charts during the program instance. I explicitly delete each wrapper by calling <code>delete</code> when I am done with it, but the managed object <code>ManagedChartClass</code> is only destructed when the program exits. This causes memory to build up that is not needed and I get 'out of memory' exceptions.</p>
  
  <p>How can I ensure that the managed objects are destructed when the wrapper is destructed?</p>