title: 'System.OutOfMemoryException' : Error creating window handle
body: <p>I'm coding a medium winforms application... every now and then on creation of certain Forms i get this exception.</p>
  
  <p>I resolved it in the past but without proper udnerstanding of what is going on...</p>
  
  <p>The puzzling part is that everything worked fine yesterday when I finnished this form and tested it, now I get this:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.OutOfMemoryException' occurred
    in System.Windows.Forms.dll</p>
    
    <p>Additional information: Error creating window handle.</p>
  </blockquote>
  
  <p>Exception is thrown in this block of code:</p>
  
  <pre><code>    public partial class PonovniIspisRacunaForm : Form
          {    
  
          DataTable dnevniPrometTable;
          DataTable kontrolnaTrakaTable;
          DataTable ukupniDnevniPrometTable;
          DataTable stavkeRacunaTable;
          ZisCode.Hibernate.Mdb.DataModel dnevniPrometDataModel;
          ZisCode.Hibernate.Mdb.DataModel kontrolnaTrakaDataModel;
          OsnovniPodaci.Porezi.Stope stope;
          string brojRacuna;
          string ZKI;
          string JIR;
          string Operator;
          //decimal ukupno = 0.00m;
  
          decimal tarifa;
          decimal kolicina;
          decimal iznos;
          decimal porez;
          decimal porez25;
          decimal porez05;
          decimal porez13;
          decimal povratnaUkupno;
          decimal osnov25;
          decimal osnov05;
          decimal osnov13;
          //decimal nabavna; 
  
          PrintDocument printDocument;
  
          public PonovniIspisRacunaForm()
          {
              InitializeComponent();
          }
  
      private void FinancijskiRekapitular_Load(object sender, EventArgs e)
              {
                  stope = new OsnovniPodaci.Porezi.Stope();
  
                  // popunjava današnji datum
                  this.dtpDatum.Value = DateTime.Today;
  
                  // Get Data Table za određeni datum:
                  dnevniPrometDataModel = ZisCode.DataModels.Get("DnevniPromet");
                  kontrolnaTrakaDataModel = ZisCode.DataModels.Get("KontrolnaTraka");
  
                  PrintSetup();
  
                  ukupniDnevniPrometTable = MergeDnevniKontrolna();
                  if (ukupniDnevniPrometTable.Rows.Count != 0)
                  {
                      FillComboBox();
              }
          }
      private void FillComboBox()
              {
                  cbBrojRacuna.DataSource = ZisCode.Methods.DataTableOperations.SortDataTable(ukupniDnevniPrometTable, "Dokument", "DESC") // orderbydescending
                      .AsEnumerable().GroupBy(row =&gt; row.Field&lt;string&gt;("Dokument")).Select(group =&gt; group.First()).CopyToDataTable(); // groupby brojRacuna-Dokument
                  cbBrojRacuna.DisplayMember = "Dokument"; // Broj Računa
                  cbBrojRacuna.ValueMember = "Dokument";
                  cbBrojRacuna.SelectedIndex = 0;
              }
  
              private void PrintSetup()
              {
                  // priprema za ispis
                  printDocument = new PrintDocument();
                  printDocument.DefaultPageSettings.PaperSize = new PaperSize("Racun size", 295, 500);
                  printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateRekapitular);
                  // uklanja poruku dialog box (printing ..) kod ispisa
                  PrintController printController = new StandardPrintController();
                  printDocument.PrintController = printController;
                  // popravlja font
                  printPreview.UseAntiAlias = true;
              }
  }
  </code></pre>
  
  <p>to be more specific this line: this.dtpDatum.Value = DateTime.Today;
  Why?? I have no idea...</p>
  
  <p>I tried seting dateTimePicker in constructor and I got different exception unrelated to window handle.</p>
  
  <p>I've read about this case on other questions but nothing really clicked for me.</p>
  
  <p><a href="https://stackoverflow.com/questions/222649/winforms-issue-error-creating-window-handle">Winforms issue - Error creating window handle</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/4216494/error-creating-window-handle">Error Creating Window Handle</a></p>
  
  <p>These two answers don't help me since answers are to ambiguous and expect me to know how dispose is supposed to work in first place. Help, I'm a n00b!</p>
  
  <p><strong>UPDATE</strong> Posted Almost all code of the class as I was asked</p>