'System.OutOfMemoryException': error while creating window handle
I am coding a medium winforms application ... from time to time when creating certain forms I get this exception.
I've solved this in the past, but without a proper understanding of what's going on ...
The amazing part is that everything went well yesterday, when I finished this form and tested it, now I get this:
An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Windows.Forms.dll
More information: Error creating window handle.
An exception is thrown in this block of 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 => row.Field<string>("Dokument")).Select(group => 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;
}
}
more specifically this line: this.dtpDatum.Value = DateTime.Today; What for?? I have no idea...
I tried to set dateTimePicker in the constructor and I got another exception not related to the window handle.
I read about this case on other questions, but there really was nothing for me.
Winforms issue - error while creating window handle
These two answers do not help me because the answers are ambiguous and suggest that I need to know how to dispose should work first. Help, I am n00b!
UPDATE Submitted Almost all of the class code as I was asked
source to share
A window handle is an unmanaged resource. There are so many of them. If you do not free these descriptors, Windows will end and you will receive exceptions.
Classes that use native HANDLE
will have to release them when they are no longer needed. These classes will implement IDisposable
. You should learn this, it is a core part of .NET and it is imperative to work with unmanaged resources like pens.
Turn on static code analysis (right click on Project
→ Properties
→ Code Analysis
and select Enable on build
and select Microsoft all rules
) and fix all violations, especially those not located IDisposable
with.
source to share
Since your exception is OutOfMemory, you may just have lost memory. Check your task manager to see how much memory you have, and I suggest running the application through some kind of memory profiler like RedGate or something similar to see where the leak is. In case of disposal take a look at this accessory for more information C # Form.Close vs Form.Dispose
source to share