Created
May 28, 2012 10:05
-
-
Save FoC-/2818295 to your computer and use it in GitHub Desktop.
Need to create class which could not been destroyed by GC
This file contains hidden or 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
| using System; | |
| namespace ConsoleApplication3 | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Undead undead = null; | |
| try | |
| { | |
| undead = new Undead(null); | |
| } | |
| catch { } | |
| for (int i = 0; i < 15; i++) | |
| { | |
| Console.Write(new WeakReference(undead).IsAlive ? "Alive " : "Dead "); | |
| Console.WriteLine(undead == null ? "No link" : "Link exist"); | |
| new HugeObject(); | |
| } | |
| Console.ReadLine(); | |
| } | |
| } | |
| class Undead | |
| { | |
| private object o; | |
| public string str = "Undead dead"; | |
| public Undead(object o) | |
| { | |
| this.o = o; | |
| Console.WriteLine("I will die"); | |
| throw new NotImplementedException(); | |
| } | |
| ~Undead() | |
| { | |
| try | |
| { | |
| new Undead(this); | |
| } | |
| catch { } | |
| if (o != null) | |
| { | |
| Console.WriteLine(((Undead) o).str); | |
| str = "Kill me plz"; | |
| } | |
| } | |
| } | |
| class HugeObject | |
| { | |
| string o = new string('0', 1024 * 1024 * 8); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment