Reputation: 134
i am getting this exception "The function evaluation was disabled because of an out of memory exception" from this line of code.
this.pbErrorSign.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
I have actually added a background image and many other images like to warning image and picture boxes instead of buttons to make attractive GUI. Program was running fine some time ago and now its giving me this.... help plz
following code is from designer.
this.pbErrorSign.BackColor = System.Drawing.Color.Transparent;
this.pbErrorSign.BackgroundImage = global::SAMS.Properties.Resources.ErrorSign3;
this.pbErrorSign.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.pbErrorSign.Location = new System.Drawing.Point(69, 121);
this.pbErrorSign.Name = "pbErrorSign";
this.pbErrorSign.Size = new System.Drawing.Size(30, 30);
this.pbErrorSign.TabIndex = 1;
this.pbErrorSign.TabStop = false;
following is the code of form named as errorDialogForm
public partial class ErrorDialogForm : Form
{
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.Capture = true;
}
public ErrorDialogForm()
{
InitializeComponent();
}
public string LabelText
{
get
{
return this.lblError.Text;
}
set
{
this.lblError.Text = value;
}
}
private void pbOkButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void pbOkButton_MouseEnter(object sender, EventArgs e)
{
this.pbOkButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.purpleOkButton));
}
private void pbOkButton_MouseLeave(object sender, EventArgs e)
{
this.pbOkButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.blueOkButton));
}
private void ErrorDialogForm_Enter(object sender, EventArgs e)
{
this.Close();
}
private void ErrorDialogForm_Deactivate(object sender, EventArgs e)
{
this.Close();
}
private void ErrorDialogForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
this.Parent = null;
e.Cancel = true;
}
}
Upvotes: 3
Views: 6087
Reputation: 942197
The function evaluation was disabled because of an out of memory exception
That's a debugger notification, it is simply telling you that it isn't going to show you anything because the program crashed with OOM. Very high odds that the debugger will crash as well when that happens. The real problem is the OOM exception you got that caused the debugger to stop the program.
this.pbOkButton.BackgroundImage = Properties.Resources.purpleOkButton;
That's the statement that caused the crash. You do this in events that fire very frequently as you move the mouse around. What is not so obvious is that this statement creates a new Bitmap object. The old ones are not being disposed. That makes the memory usage of your program rapidly climb with low odds that the garbage collector can do anything about it because you don't allocate any other objects. The OOM kaboom is pretty inevitable.
The proper fix is to create these bitmaps only once:
private Image purpleOk;
private Image blueOk;
public ErrorDialogForm()
{
InitializeComponent();
purpleOk = Properties.Resources.purpleOkButton;
blueOk = Properties.Resources.blueOkButton;
pbOkButton.BackgroundImage = blueOk;
}
private void pbOkButton_MouseEnter(object sender, EventArgs e)
{
this.pbOkButton.BackgroundImage = purpleOk;
}
private void pbOkButton_MouseLeave(object sender, EventArgs e)
{
this.pbOkButton.BackgroundImage = blueOk;
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
purpleOk.Dispose();
blueOk.Dispose();
base.OnFormClosed(e);
}
Upvotes: 2