Form with picture box not being released - c#

I am trying to chase a memory leak in a multiform C#.Net application, on Windows 7, VS 2008.
I found this SO post, which indicates that the second form's finalizer should be automatically called
this.Dispose() doesn't release memory used by Form after closing it.
However, it isn't working for me.
Each time a pop up the second form (which contains a large string, and a PictureBox), the amount of memory the app uses increases, even when I force a GC. Interestingly, the String's finalizer IS being called.
I've add a log to both form's Dispose methods; Form2's dispose is being called.
The code is
Form1 (main form)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MemoryLeakTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
~Form1()
{
System.Diagnostics.Debugger.Log(0, "", "Form1.Destructor has been called.\n");
}
private void GC_Click(object sender, EventArgs e)
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
private void OpenForm_Click(object sender, EventArgs e)
{
Form secondForm = null;
if (secondForm == null)
{
secondForm = new Form2();
secondForm.Show();
}
secondForm = null;
}
private void UpdateMemory_Click(object sender, EventArgs e)
{
long memory = System.GC.GetTotalMemory(false);
this.MemoryUsage.Text = "Memory usage: " +
String.Format("{0:n}", memory) +
" bytes";
}
private void AllocateString_Click(object sender, EventArgs e)
{
// StringWrapper wrapper = new StringWrapper();
}
}
public class StringWrapper
{
String str = new String('*', 1024 * 1024);
~StringWrapper()
{
System.Diagnostics.Debugger.Log(0, "", "StringWrapper finalizer has been called.\n");
}
}
}
and Form2 is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MemoryLeakTest
{
public partial class Form2 : Form
{
StringWrapper wrapper = new StringWrapper();
public Form2()
{
InitializeComponent();
this.pictureBox1.Image = Image.FromFile("C:/Windows/Web/Wallpaper/Windows/img0.jpg");
}
~Form2()
{
System.Diagnostics.Debugger.Log(0, "", "Form2.Finalizer has been called.\n");
}
}
}
I haven't included the designer generated code, but can add it if need be

Related

Why does the following code produce Object Disposed Exception

I am playing around in visual studio and getting to know C# better. I am coming from an intermediate background knowledge of Java.
I have produced a very simple windows form application. The user clicks on a button, the button takes them to another screen, the user types into a textbox and presses a button in which that button will display what the user typed in; in the form. This is the code:
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
Form2 userinputForm;
public Form2 getSetForm2 {
get { return userinputForm; }
set { userinputForm = value; }
}
Form1 homeFormObj;
public Form1 getSetForm1 {
get { return homeFormObj; }
set { homeFormObj = value; }
}
public Form1()
{
InitializeComponent();
getSetForm2 = new Form2();
getSetForm1 = this;
getSetForm2.formOnePublicObj = getSetForm1;
}
internal void displayUserInput(string name)
{
Label l = new Label();
l.Text = name;
panel1.Controls.Add(l);
}
private void button1_Click(object sender, EventArgs e)
{
userinputForm.Show();
}
}
}
Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
Form1 formOneObj;
public Form1 formOnePublicObj {
get { return formOneObj; }
set { formOneObj = value; }
}
public Form2()
{
InitializeComponent();
}
List<string> userinputs = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
formOnePublicObj.displayUserInput(name);
}
}
}
The error occurs the second time the user presses the button to go to form2. it occurs on the .show() method.
(P.S I coded like this to see how I can pass data from one windows form to another hence the getters and setters on the form objects).
Well, userinputform is never set and so is null. As such I don't understand why it works the first time unless this isn't actually your code pasted in.
It's probably because you're closing the second form, which is destroying it therefore you can't show it again. Each time you click the button in form1 create a new form2:
getSetForm2 = new Form2();
getSetForm1 = this;
getSetForm2.formOnePublicObj = getSetForm1;

Get/Set Variable

the goal of the program is to be able to use the get and set methods of variables.
I have this code in a project C#:
Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int c = 0;
public int a { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
a = 5;
Form2 f2 = new Form2();
f2.b = a;
f2.Show();
}
}
}
and in a Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
MessageBox.Show(Convert.ToString(b));
}
}
}
This code not work because the value of b should be 5, but during execution value 0;
any solution?
You can't expect values of one instance to magically propagate to every other instance.
So doing this:
Form1 f1 = new Form1();
int b = f1.a;
Is always going to be 0. You created a new instance, and nothing has happened to it! If you want to get the existing form's value (where the button was presumably clicked) you need to pass it to Form2 somehow.
You can:
Pass it on the constructor of Form2
Set up a service that holds the data instead
Probably about a million other approaches
private void button1_Click(object sender, EventArgs e)
{
a = 5;
Form2 f2 = new Form2();
f2.b=a;
f2.Show();
}
public partial class Form2 : Form
{
public int b;
public Form2()
{
InitializeComponent();
}
}
You want to pass the data before showing the second form instead of after the fact.

How to change user control label.text from a form

Right so I have a user control called "ModbusMaster" and a form with literally a single button on it..
When I click the button I want to change the text of a label on my control..
However nothing happens..
Here is the main form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ModbusMaster_2._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ModbusMaster mb = new ModbusMaster();
public void button1_Click(object sender, EventArgs e)
{
mb.openPort("wooooo");
}
}
}
I am calling the method openPort and passing the string "wooo" to it..
here is my control
The text does not get updated :(:(:(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace ModbusMaster_2._0
{
public partial class ModbusMaster : UserControl
{
string portName = "COM1"; //default portname
int timeOut = 300; //default timeout for response
SerialPort sp = new SerialPort();
public ModbusMaster()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
portLabel.Text = portName;
}
public void openPort(string port)
{
statusLabel.Text = port;
}
/*
* Properties
*/
public string SerialPort //Set portname
{
get { return portName; }
set { portName = value;}
}
public int TimeOut //Set response timeout
{
get { return timeOut; }
set { timeOut = value; }
}
}
}
I think you must have two instances of ModbusMaster.
One of them is the one you can see on the display, and is NOT being updated.
The other one is the one you create in class Form1 with the line of code:
ModbusMaster mb = new ModbusMaster();
That is the one you are modifying, but it isn't the displayed one (I cannot see anywhere that you can be displaying that).
What you need to do is use the reference to the actual displayed one instead when you call mb.openPort("wooooo");
[EDIT]
Thinking about it - it's possible that you haven't instantiated another user control at all.
Did you use Visual Studio's Form Designer to add the user control to your main form? I had assumed that you did, but now I realise that might not be the case.
If not, you should do that, give it the name mb and remove the line that says ModbusMaster mb = new ModbusMaster(); and it might work without you having to make more extensive changes.
You are creating your UserControl but not assigning it to your Form's Control Collection. Try something like this in your Constructor.
namespace ModbusMaster_2._0
{
public partial class Form1 : Form
{
ModbusMaster mb = new ModbusMaster();
public Form1()
{
InitializeComponent();
this.Controls.Add(mb); //Add your usercontrol to your forms control collection
}
public void button1_Click(object sender, EventArgs e)
{
mb.openPort("wooooo");
}
}
}

visual c# 2010 playing sound in form 1 (From resorce file)

OK i am trying to make my form 1 application play a .wav file i have imported to my resource file i have serched for this online but all i can find is
Sub PlayBackgroundSoundFile()
My.Computer.Audio.Play("C:\Waterfall.wav", _
AudioPlayMode.Background) End Sub
but i believe this is for a console application if not not matter where i put the code i get an lots of errors
Here is the code i am working with:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
var myForm = new Form1();
myForm.Show();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
}
private class form1_load
{
}
}
}
What do i have to add and where bare in mind i have never used any method like this before
if you with to just edit the code the name of the file is sound.wav
I just did it myself: created simple form with one button and created event to this button. Contents of Form1 class is:
using System;
using System.Media;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btPlay_Click(object sender, EventArgs e)
{
string path;
using (var dlg = new OpenFileDialog())
{
dlg.Multiselect = false;
dlg.Filter = "WAV files|*.wav";
if (dlg.ShowDialog() == DialogResult.Cancel) return;
path = dlg.FileName;
}
SoundPlayer sp = new SoundPlayer(path);
sp.Play();
}
}
}
And it works perfectly. Try this on, an if you're still gonna be having errors, you could post here which errors exactly. It would help me in helping you.

How to pass a string to child form?

Basically what I'm trying to do is I have a string on the main form that pulls its value from a textbox.
I then generate a modal version of a second form and want to have that string (or the main forms textbox1.text value) usable in the second form for processes.
Main Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Tool{
public partial class MainForm : Form
{
public string hostname;
public MainForm()
{
InitializeComponent();
textBox1.Text = hostname;
}
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.ShowDialog();
}
}
}
'
Child Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Tool
{
public partial class SiteForm : Form
{
public string hostname {get; set; }
public SiteForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = this.hostname;
}
}
}
Any suggestions on how I can do this? I know there has to be a simpler way, sorry I'm still a bit of a noob and am trying to teach myself C# as I go.
The result is when I click the label on the child form it is blank, because of this I am able to deduce that the string isn't passing between the two forms correctly.
The simplest way is to pass it in the constructor of the Child form, for example:
private string _hostname = "";
...
public SiteForm(string hostname)
{
_hostname = hostname;
InitializeComponent();
}
Try hooking into your child form's Load event and set the value of its hostname property in an event handler on your main form.
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.Load += new EventHandler(frmsite_Load);
frmsite.ShowDialog();
}
public void frmsite_Load(object sender, EventArgs e)
{
SiteForm frmsite = sender as SiteForm;
frmsite.hostname = this.hostname;
}

Categories

Resources