public static void AlsoNeedHelp() { using (RwLck lck = TheVault.Lock()) { IllegalWrapper wrapper = default; wrapper.SetLockedResource(in lck); lck.AppendLine("Goodbye cruel world!"); Console.WriteLine(wrapper.WrappedLockedResource.ToString()); } }
//internal static void ShowMoreDetailedBug92Fix() //{ // //protected resources are now decorated with the NoCopy attribute. // //If a ref struct contains a field of the same type as the protected resource, // //it is now prohibited to allow these two to overlap in scope. // //ref struct wrapper with containing non-static field of type StringBuilderRwLockedResource // RwLockWrapper wrapperCopy=default; // { // using (var lck = TheVault.Lock()) // { // lck.AppendLine("We don't need no education ... "); // //Bug 92 fix -- uncommenting the following line triggers compilation error // //bug 92 fix -- a ref struct that has a non-static field of the same type as a locked // //bug 92 fix -- resource object, may not appear in the same scope as the locked // //bug 92 fix -- resource object, hence, following line will cause compilation error. // using (wrapperCopy = new RwLockWrapper(in lck, "Foobar")) // { // Console.WriteLine(wrapperCopy.ToString()); // } // } // } // //the locked resource has been returned to vault and this line causes unsynchronized access // Console.WriteLine(wrapperCopy.ToString()); //} //internal static void ShowMoreDetailedBug92AnotherVariation() //{ // //protected resources are now decorated with the NoCopy attribute. // //If a ref struct contains a field of the same type as the protected resource, // //it is now prohibited to allow these two to overlap in scope. // //ref struct wrapper with containing non-static field of type StringBuilderRwLockedResource // string fizz; // { // using (var lck = TheVault.Lock()) // { // lck.AppendLine("We don't need no education ... "); // //Bug 92 fix -- uncommenting the following line triggers compilation error // //bug 92 fix -- a ref struct that has a non-static field of the same type as a locked // //bug 92 fix -- resource object, may not appear in the same scope as the locked // //bug 92 fix -- resource object, hence, following line will cause compilation error. // var wrapperCopy = new RwLockWrapper(in lck, "Foobar"); // fizz = wrapperCopy.ToString(); // } // } // //the locked resource has been returned to vault and this line causes unsynchronized access // Console.WriteLine(fizz); //} internal static void Bug92StillAProblem() { using var lck = TheVault.Lock(); lck.AppendLine("We don't need no education ... "); //Bug 92 fix -- uncommenting the following line triggers compilation error //bug 92 fix -- a ref struct that has a non-static field of the same type as a locked //bug 92 fix -- resource object, may not appear in the same scope as the locked //bug 92 fix -- resource object, hence, following line will cause compilation error. //using RwLockWrapper wrapperCopy = new RwLockWrapper(in lck, "Foobar"); //Console.WriteLine(wrapperCopy.ToString()); }
public static void HelpMe() { using RwLck lck = TheVault.Lock(); //bad IllegalWrapper wrapper = IllegalWrapper.CreateCopyInSneakyWay(in lck, DateTime.Now); //also bad wrapper.SetLockedResource(in lck); lck.AppendLine("Hi mom!"); Console.WriteLine(wrapper.WrappedLockedResource.ToString()); }