title: Using custom new-handler with Eigen library
body: <p>I am trying to use a custom new-handler to respond to out of memory errors in a program where I use the <a href="http://eigen.tuxfamily.org/" rel="nofollow">Eigen</a> library.</p>
  
  <p>When I solve an instance, I cache the results (a large <em>dynamic</em> size vector), so if I need to solve that instance again I already have the results. After a while it may happen that too many instances are cached, so there is not enough memory left during computation. In this case I would empty the cache (or better remove some instances).</p>
  
  <p>The simplest way I came up with is to have a global variable <code>std::map&lt;InputType, ResultType&gt; cache</code>, and to install a new-handler that empties it (unless it is already empty, then it falls back on the default handler). In this way when an allocation fails my handler frees some memory and returns, so another attempt is done to allocate memory.</p>
  
  <p>However it seems that such handler is never called, instead a <code>std::bad_alloc</code> exception is always thrown.</p>
  
  <p>I tried this example taken from <a href="http://en.cppreference.com/w/cpp/memory/new/set_new_handler" rel="nofollow">here</a>. When using <code>std::vector&lt;int&gt;</code> the custom handler is correctly called, and "Memory allocation failed, terminating" is printed. When using <code>Eigen::VectorXi</code> I only got a <code>std::bad_alloc</code> exception.</p>
  
  <pre><code>#include &lt;Eigen/Dense&gt;
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;new&gt;
  
  void handler()
  {
      std::cout &lt;&lt; "Memory allocation failed, terminating\n";
      std::set_new_handler(nullptr);
  }
  
  int main()
  {
      std::set_new_handler(handler);
      try {
          while(true) {
              const unsigned long int Size = 100000000ul;
  
              // Either one of the following:
  
              // new Eigen::VectorXi(Size);
              new std::vector&lt;int&gt;(Size);
          }
      } catch (const std::bad_alloc &amp; e) {
          std::cout &lt;&lt; e.what() &lt;&lt; '\n';
      }
  }
  </code></pre>
  
  <p>I wonder why this happens, perhaps Eigen does not use <code>operator new</code> to allocate memory? I could not find anything in the documentation. Is there a way to use this technique with Eigen? Or must I catch the exception at high level, clear the cache, and perform the computation again?</p>