Reputation: 1004
I'm coding a medium winforms application... every now and then on creation of certain Forms i get this exception.
I resolved it in the past but without proper udnerstanding of what is going on...
The puzzling part is that everything worked fine yesterday when I finnished this form and tested it, now I get this:
An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Windows.Forms.dll
Additional information: Error creating window handle.
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;
}
}
to be more specific this line: this.dtpDatum.Value = DateTime.Today; Why?? I have no idea...
I tried seting dateTimePicker in constructor and I got different exception unrelated to window handle.
I've read about this case on other questions but nothing really clicked for me.
Winforms issue - Error creating window handle
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!
UPDATE Posted Almost all code of the class as I was asked
Upvotes: 3
Views: 2185
Reputation: 1004
The problem was private void
dtpDatum_TextChanged(object sender, EventArgs e)
{
ukupniDnevniPrometTable = MergeDnevniKontrolna();
FillComboBox();
}
I guess this event was fired up too many times and caused memory leak
Upvotes: 0
Reputation: 77304
A window handle is an unmanaged resource. There are only so many of them. If you don't free those handles, Windows will run out and you will get exceptions.
Classes that use native HANDLE
s will need to free them when they are no longer needed. Those classes will implement IDisposable
. You should look into this, it's a core part of .NET and crucial to working with unmanaged resources like handles.
Turn on your static code analysis (Right click on Project
-> Properties
-> Code Analysis
and select Enable on build
and pick Microsoft all rules
) and fix all violations, especially those non-disposed IDisposable
s.
Upvotes: 4
Reputation: 121
Since your exception is OutOfMemory it may be that you are just out of memory. Check task manager to see how much memory you have available and I would suggest to run your app through some memory profiler like RedGate or something similar to see where the leak is. In case of dispose take a look at this asnwer for more info C# Form.Close vs Form.Dispose
Upvotes: 1