Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Can anybody tell me why do i get such exception when a new form is loaded with controls in windows form platform. I have got 2 forms, so from form1 i am passing few data to form2 & trying to load the form but i get this exception. In form2 i have 27 labels, 29 rectangles, 2 datagridview tables & 6 buttons. Is, these many controls too much for the system to hanlde...? How to resolve the issue..? Thanks in advance..
Posted
Comments
tumbledDown2earth 10-May-13 4:49am    
I presume you have leaking memory in your code... Please profile your application to see the memory footprint when it crashes
CHill60 10-May-13 4:55am    
Exactly when does this happen (i.e. is it when the first form is loading, do you actually get to see the form?). Do you have any code in the form load event?
Jagadisha_Ingenious 10-May-13 4:59am    
@CHill60 I have few code lines in form load event too.... One of them is to change the shape of the button, other for default settings of the datagridview. This error occurs during switching from form1 to form2 exactly @ form2.show(); line of the code.
ZurdoDev 10-May-13 8:22am    
You need to reply to the comment so that the user will be notified. Please do not add a comment to your own question.
Jagadisha_Ingenious 10-May-13 5:02am    
@tumbledDown2earth:
The Form1 button click event is shown below:


private void glassButton1_Click(object sender, EventArgs e)
{
string projectName = comboBox1.Text;
string projectName1 = comboBox2.Text;
string projectName2 = comboBox3.Text;

Form1 Form1 = new Form1(projectName, projectName1, projectName2);
Form1.MdiParent = MdiParent;
Form1.WindowState = FormWindowState.Maximized;
Form1.Show();
this.Close();
}

Form2 load event code is shown below:
private void Form2_Load(object sender, EventArgs e)
{
if (ESP == "2")
{
slaves = 96;
button3.Hide();
button4.Hide();

for (int i = 0; i <= 96; i++)
{
slaveaddress[i] = i;
}

}

if (ESP == "3")
{
slaves = 144;
button4.Hide();
for (int i = 0; i <= 144; i++)
{
slaveaddress[i] = i;
}
}

System.Drawing.Drawing2D.GraphicsPath g_path = new System.Drawing.Drawing2D.GraphicsPath();
Point[] myarr = { new Point(10, 35), new Point(10, 95), new Point(30, 115), new Point(120, 115), new Point(140, 95), new Point(140, 35), new Point(120, 15), new Point(30, 15) };
g_path.AddPolygon(myarr);
button1.BackColor = System.Drawing.Color.LimeGreen;
button1.Region = new Region(g_path);
button2.BackColor = System.Drawing.Color.LimeGreen;
button2.Region = new Region(g_path);
button3.BackColor = System.Drawing.Color.LimeGreen;
button3.Region = new Region(g_path);
button4.BackColor = System.Drawing.Color.LimeGreen;
button4.Region = new Region(g_path);

lineShape2.Hide();
lineShape4.Hide();
lineShape5.Hide();
lineShape11.Hide();


BSView1.Rows.Clear();
BSView1.Columns.Clear();

BSView2.Rows.Clear();
BSView2.Columns.Clear();

BSView1.ColumnCount = 1;
BSView1.Columns[0].Name = "FIELDS";
BSView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
BSView1.Columns[0].Width = 100;
this.BSView1.Columns["FIELDS"].ReadOnly = true;

BSView2.ColumnCount = 1;
BSView2.Columns[0].Name = "FIELDS";
BSView2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
BSView2.Columns[0].Width = 100;
this.BSView2.Columns["FIELDS"].ReadOnly = true;

int height = 0;
for (int row = 0; row <= (field) - 1; row++)
{

// We need to save each row as an array
string[] currentColumn = new string[2];
BSView1.Rows.Add(currentColumn);
BSView1.Rows[row].DefaultCellStyle.BackColor = Color.LimeGreen;
BSView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
BSView1.Rows[row].Cells[0].Value = "FIELD" + "-" + (row + 1);
height += BSView1.Rows[row].Height;
// BSView1.Rows[row].Cells[1].Value = "Field"+(row+1+(field/2));
BSView1.ClearSelection();

BSView2.Rows.Add(currentColumn);
BSView2.Rows[row].DefaultCellStyle.BackColor = Color.LimeGreen;
BSView2.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
BSView2.Rows[row].Cells[0].Value = "FIELD" + "-" + (row + 1);
//BSView2.Rows[row].Cells[1].Value = "Field"+(row+1+(field/2));
BSView2.ClearSelection();
}
height += BSView1.

It has nothing to do with the number of controls on your form. It's more than likely you're creating new drawing objects (Pen, Brush, Graphics, ...) and not disposing them when you're done using them.

You can get "OutOfMemory" exceptions for a number of reasons that don't have anynthing to do with memory. In your case, it's probably because you have a "handle" leak in your code.

Open Task Manager, click on the Processes tab, then add the column "Handles" to the display. Start your app and look for it in the Processes list. Watch the Handles number as you use the app. If it starts to exceed a couple thousand handles, you've got a leak in your code that will eventually cause an "OutOfMemory" exception.
 
Share this answer
 
These type of error occur when there are to much data to process.So for that use "USING" keyword which dispose the object when its used.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900