Skip to content

Instantly share code, notes, and snippets.

@coxsim
Created May 24, 2011 03:19
Show Gist options
  • Save coxsim/988093 to your computer and use it in GitHub Desktop.
Save coxsim/988093 to your computer and use it in GitHub Desktop.
Loop variable not getting collected
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Foo GetFoo(int i)
{
if (i == 0)
return new Foo(i);
lock (this)
{
Monitor.Wait(this);
return new Foo(i);
}
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
while (true)
{
var foo = GetFoo(i++);
Console.WriteLine(foo.i);
foo = null;
if (i == 2)
return;
}
}
}
class Foo
{
public readonly int i;
public Foo(int i)
{
this.i = i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment