static void TestRefcounted () { Refcounted ref1, ref2; IntPtr handle; Console.WriteLine ("Testing Refcounted new/free"); ref1 = new Refcounted (); if (!ref1.Owned) Error ("Newly-created Refcounted is not Owned"); handle = ref1.Handle; ref1.Dispose (); Opaquetest.ExpectError = true; ref1 = new Refcounted (handle); if (!Opaquetest.Error) Error ("Didn't get expected ref error resurrecting ref1."); if (!ref1.Owned) Error ("IntPtr-created Refcounted is not Owned"); Opaquetest.ExpectError = true; if (ref1.Serial != Refcounted.LastSerial) Error ("Serial mismatch. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); // We caused it to take a ref on the "freed" underlying object, so // undo that now so it doesn't cause an error later when we're not // expecting it. Opaquetest.ExpectError = true; ref1.Dispose (); Opaquetest.Error = false; Console.WriteLine ("Testing Refcounted leak/non-free"); ref1 = new Refcounted (); ref1.Owned = false; handle = ref1.Handle; ref1.Dispose (); ref1 = new Refcounted (handle); if (Opaquetest.Error) Error ("Non-owned ref was freed by gtk#"); if (ref1.Serial != Refcounted.LastSerial) Error ("Serial mismatch. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); if (Opaquetest.Error) Error ("Non-owned ref was freed by gtk#"); Console.WriteLine ("Testing non-owned return."); ref1 = new Refcounted (); ref2 = new Refcounted (); ref1.Friend = ref2; if (Opaquetest.Error) Error ("Memory error after setting ref1.Friend."); if (ref2.Refcount != 2) Error ("Refcount wrong for ref2 after setting ref1.Friend. Expected 2, Got {0}", ref2.Refcount); ref2.Dispose (); ref2 = ref1.Friend; if (ref2.Serial != Refcounted.LastSerial || Opaquetest.Error) Error ("Error reading ref1.Friend. Expected {0}, Got {1}", Refcounted.LastSerial, ref2.Serial); if (ref2.Refcount != 2 || Opaquetest.Error) Error ("Refcount wrong for ref2 after reading ref1.Friend. Expected 2, Got {0}", ref2.Refcount); if (!ref2.Owned) Error ("ref2 not Owned after being read off ref1.Friend"); ref1.Dispose (); ref2.Dispose (); if (Opaquetest.Error) Error ("Memory error after freeing ref1 and ref2."); Console.WriteLine ("Testing returning a Gtk#-owned refcounted from C# to C"); ret_ref = new Refcounted (); ref1 = Refcounted.Check (new Gtksharp.RefcountedReturnFunc (ReturnRefcounted), new Gtksharp.GCFunc (GC)); if (ref1.Serial != Refcounted.LastSerial || Opaquetest.Error) Error ("Error during Refcounted.Check. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); ref1.Dispose (); if (Opaquetest.Error) Error ("Memory error after clearing ref1."); Console.WriteLine ("Testing returning a Gtk#-owned refcounted to a C method that will free it"); ret_ref = new Refcounted (); ref1 = Refcounted.CheckUnref (new Gtksharp.RefcountedReturnFunc (ReturnRefcounted), new Gtksharp.GCFunc (GC)); if (Opaquetest.Error) Error ("Error during Refcounted.CheckUnref."); Opaquetest.ExpectError = true; if (ref1.Serial != Refcounted.LastSerial) Error ("Error during Refcounted.CheckUnref. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); if (!Opaquetest.Error) Error ("Didn't get expected error accessing ref1.Serial!"); Opaquetest.ExpectError = true; ref1.Dispose (); if (!Opaquetest.Error) Error ("Didn't get expected double free on ref1 after CheckUnref!"); Console.WriteLine ("Testing leaking a C-owned refcounted"); ret_ref = new Refcounted (); ret_ref.Owned = false; ref1 = Refcounted.Check (new Gtksharp.RefcountedReturnFunc (ReturnRefcounted), new Gtksharp.GCFunc (GC)); if (ref1.Serial != Refcounted.LastSerial || Opaquetest.Error) Error ("Error during Refcounted.Check. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); handle = ref1.Handle; ref1.Dispose (); if (Opaquetest.Error) Error ("Memory error after disposing ref1."); ref1 = new Refcounted (handle); if (ref1.Serial != Refcounted.LastSerial || Opaquetest.Error) Error ("Failed to leak ref1. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); Console.WriteLine ("Testing handing over a C-owned refcounted to a C method that will free it"); ret_ref = new Refcounted (); ret_ref.Owned = false; ref1 = Refcounted.CheckUnref (new Gtksharp.RefcountedReturnFunc (ReturnRefcounted), new Gtksharp.GCFunc (GC)); if (Opaquetest.Error) Error ("Error during Refcounted.CheckUnref."); Opaquetest.ExpectError = true; if (ref1.Serial != Refcounted.LastSerial) Error ("Error during Refcounted.CheckUnref. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); if (!Opaquetest.Error) Error ("Didn't get expected error accessing ref1.Serial!"); ref1.Dispose (); if (Opaquetest.Error) Error ("Double free on ref1!"); }
static void TestRefcounted() { Refcounted ref1, ref2; IntPtr handle; Console.WriteLine("Testing Refcounted new/free"); ref1 = new Refcounted(); if (!ref1.Owned) { Error("Newly-created Refcounted is not Owned"); } handle = ref1.Handle; ref1.Dispose(); Opaquetest.ExpectError = true; ref1 = new Refcounted(handle); if (!Opaquetest.Error) { Error("Didn't get expected ref error resurrecting ref1."); } if (!ref1.Owned) { Error("IntPtr-created Refcounted is not Owned"); } Opaquetest.ExpectError = true; if (ref1.Serial != Refcounted.LastSerial) { Error("Serial mismatch. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); } // We caused it to take a ref on the "freed" underlying object, so // undo that now so it doesn't cause an error later when we're not // expecting it. Opaquetest.ExpectError = true; ref1.Dispose(); Opaquetest.Error = false; Console.WriteLine("Testing Refcounted leak/non-free"); ref1 = new Refcounted(); ref1.Owned = false; handle = ref1.Handle; ref1.Dispose(); ref1 = new Refcounted(handle); if (Opaquetest.Error) { Error("Non-owned ref was freed by gtk#"); } if (ref1.Serial != Refcounted.LastSerial) { Error("Serial mismatch. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); } if (Opaquetest.Error) { Error("Non-owned ref was freed by gtk#"); } Console.WriteLine("Testing non-owned return."); ref1 = new Refcounted(); ref2 = new Refcounted(); ref1.Friend = ref2; if (Opaquetest.Error) { Error("Memory error after setting ref1.Friend."); } if (ref2.Refcount != 2) { Error("Refcount wrong for ref2 after setting ref1.Friend. Expected 2, Got {0}", ref2.Refcount); } ref2.Dispose(); ref2 = ref1.Friend; if (ref2.Serial != Refcounted.LastSerial || Opaquetest.Error) { Error("Error reading ref1.Friend. Expected {0}, Got {1}", Refcounted.LastSerial, ref2.Serial); } if (ref2.Refcount != 2 || Opaquetest.Error) { Error("Refcount wrong for ref2 after reading ref1.Friend. Expected 2, Got {0}", ref2.Refcount); } if (!ref2.Owned) { Error("ref2 not Owned after being read off ref1.Friend"); } ref1.Dispose(); ref2.Dispose(); if (Opaquetest.Error) { Error("Memory error after freeing ref1 and ref2."); } Console.WriteLine("Testing returning a Gtk#-owned refcounted from C# to C"); ret_ref = new Refcounted(); ref1 = Refcounted.Check(new Gtksharp.RefcountedReturnFunc(ReturnRefcounted), new Gtksharp.GCFunc(GC)); if (ref1.Serial != Refcounted.LastSerial || Opaquetest.Error) { Error("Error during Refcounted.Check. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); } ref1.Dispose(); if (Opaquetest.Error) { Error("Memory error after clearing ref1."); } Console.WriteLine("Testing returning a Gtk#-owned refcounted to a C method that will free it"); ret_ref = new Refcounted(); ref1 = Refcounted.CheckUnref(new Gtksharp.RefcountedReturnFunc(ReturnRefcounted), new Gtksharp.GCFunc(GC)); if (Opaquetest.Error) { Error("Error during Refcounted.CheckUnref."); } Opaquetest.ExpectError = true; if (ref1.Serial != Refcounted.LastSerial) { Error("Error during Refcounted.CheckUnref. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); } if (!Opaquetest.Error) { Error("Didn't get expected error accessing ref1.Serial!"); } Opaquetest.ExpectError = true; ref1.Dispose(); if (!Opaquetest.Error) { Error("Didn't get expected double free on ref1 after CheckUnref!"); } Console.WriteLine("Testing leaking a C-owned refcounted"); ret_ref = new Refcounted(); ret_ref.Owned = false; ref1 = Refcounted.Check(new Gtksharp.RefcountedReturnFunc(ReturnRefcounted), new Gtksharp.GCFunc(GC)); if (ref1.Serial != Refcounted.LastSerial || Opaquetest.Error) { Error("Error during Refcounted.Check. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); } handle = ref1.Handle; ref1.Dispose(); if (Opaquetest.Error) { Error("Memory error after disposing ref1."); } ref1 = new Refcounted(handle); if (ref1.Serial != Refcounted.LastSerial || Opaquetest.Error) { Error("Failed to leak ref1. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); } Console.WriteLine("Testing handing over a C-owned refcounted to a C method that will free it"); ret_ref = new Refcounted(); ret_ref.Owned = false; ref1 = Refcounted.CheckUnref(new Gtksharp.RefcountedReturnFunc(ReturnRefcounted), new Gtksharp.GCFunc(GC)); if (Opaquetest.Error) { Error("Error during Refcounted.CheckUnref."); } Opaquetest.ExpectError = true; if (ref1.Serial != Refcounted.LastSerial) { Error("Error during Refcounted.CheckUnref. Expected {0}, Got {1}", Refcounted.LastSerial, ref1.Serial); } if (!Opaquetest.Error) { Error("Didn't get expected error accessing ref1.Serial!"); } ref1.Dispose(); if (Opaquetest.Error) { Error("Double free on ref1!"); } }