Created
May 24, 2011 03:19
-
-
Save coxsim/988093 to your computer and use it in GitHub Desktop.
Loop variable not getting collected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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