title: Win32Exception Not enough storage is available to process this command
body: <p>Through my automated crash collection for <a href="http://www.maxto.net" rel="noreferrer">MaxTo</a> I got the following crash report:</p>
  
  <pre><code>V8.12.0.0 - System.ComponentModel.Win32Exception - :Void UpdateLayered():0
  Version: MaxTo8.12.0.0
  Exception: System.ComponentModel.Win32Exception
  Error message: Not enough storage is available to process this command
  Stack trace: 
    at System.Windows.Forms.Form.UpdateLayered()
    at System.Windows.Forms.Form.OnHandleCreated(EventArgs e)
    at System.Windows.Forms.Control.WmCreate(Message&amp; m)
    at System.Windows.Forms.Control.WndProc(Message&amp; m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
    at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)
    at System.Windows.Forms.Form.WmCreate(Message&amp; m)
    at System.Windows.Forms.Form.WndProc(Message&amp; m)
    at MaxTo.MainForm.WndProc(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  </code></pre>
  
  <p>Another stacktrace:</p>
  
  <pre><code>Version: MaxTo2009.9.0.0
  Exception: System.ComponentModel.Win32Exception
  Error message: Not enough storage is available to process this command
  Stack trace: 
    at System.Windows.Forms.Form.UpdateLayered()
    at System.Windows.Forms.Form.OnHandleCreated(EventArgs e)
    at System.Windows.Forms.Control.WmCreate(Message&amp; m)
    at System.Windows.Forms.Control.WndProc(Message&amp; m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
    at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)
    at System.Windows.Forms.Form.WmCreate(Message&amp; m)
    at System.Windows.Forms.Form.WndProc(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  </code></pre>
  
  <p>In this latest stack trace there is no reference to MaxTo at all, and 90% of the crashes I get are with stack traces similar to the above.</p>
  
  <p>Reading around on the net I find that this error is usual if you forget to release or dispose variables. When looking through my <code>WndProc</code>, which seems to sometimes have the problem pass through, I cannot find a single place that hangs on to references to any objects. All but one of the variables are local to WndProc, and should therefore be garbage collected when the method terminates.</p>
  
  <pre><code>protected override void WndProc(ref Message m)
  {
      base.WndProc(ref m); // I'm assuming the first trace can be caught here
      IntPtr hwnd = m.WParam;
      // Our hook tells us something got maximized
      if (Win32Import.UWM_MAXIMIZE == (UInt32)m.Msg)
      {
          // Figure out if we are temporarily disabled or using alternative profiles
          KeyStateInfo keyState = KeyboardInfo.GetKeyState(Settings.AlternativeProfileKey);
          Rectangle r = FindRectangle(MousePosition, (Settings.EnableAlternativeProfile &amp;&amp; keyState.IsPressed ? AlternativeRegions : Regions));
          // Did we find a rectangle to place it in?
          if (r != Rectangle.Empty)
          {
              Rectangle position = Win32Import.GetWindowRectangle(hwnd);
              Rectangle previousPos = GetLocation(hwnd);
              if (position == r &amp;&amp; previousPos != Rectangle.Empty)
              {
                  // We are restoring the original position
                  Win32Import.SetWindowPos(hwnd, IntPtr.Zero, previousPos.X, previousPos.Y, previousPos.Width, previousPos.Height, Win32Import.SWP_NOZORDER | Win32Import.SWP_NOSENDCHANGING);
              }
              else
              {
                  // We are maximizing to a region
                  Win32Import.ShowWindow(hwnd, Win32Import.WindowShowStyle.Restore);
                  Win32Import.SetWindowPos(hwnd, IntPtr.Zero, r.X, r.Y, r.Width, r.Height, Win32Import.SWP_NOZORDER | Win32Import.SWP_NOSENDCHANGING);
                  // Make sure we remember this location
                  RememberLocation(hwnd, position);
              }
          }
      }
      else if (MaxTo64WindowHandleMessage == m.Msg)
      {
          // Store the window handle of our 64-bit subprocess
          SubProcess64WindowHandle = m.WParam;
      }
  }
  </code></pre>
  
  <p>I have not been able to reproduce the error, even while running the program over multiple days.</p>
  
  <p>My assumption is that the system is low on either unfragmented memory or GDI handles, but I cannot confirm this anywhere. There does not seem to be any good documentation on this error.</p>
  
  <p>Any ideas what else it could be? Can I do anything to prevent this error?</p>
  
  <p><strong>Update</strong>: The question was reopened with more stack traces, because of the lack of a decent solution. Simply ignoring it does not solve the problem.</p>