beefytech / Beef

Beef Programming Language
http://www.beeflang.org
Other
2.49k stars 128 forks source link

Access to deleted object not identified #1665

Open disarray2077 opened 2 years ago

disarray2077 commented 2 years ago

In the code below, dlg should have triggered the error "Attempt to access deleted object", since this was deleted at that point.

    public static class Program
    {
        class TestDeleted
        {
            public bool test;

            public delegate bool() GetDlg()
            {
                return new () => { return test; };
            }
        }

        public static void Main()
        {
            let obj = new TestDeleted();
            let dlg = obj.GetDlg();
            delete obj;
            Console.WriteLine(dlg()); // No error!
            Console.WriteLine(obj.test); // Attempt to access deleted object
        }
    }

Tested with: https://github.com/beefytech/Beef/commit/1c7b7df25fd8f72b25f29bec13d99221c9f4b268

bfiete commented 2 years ago

The fundamental problem is that the delegate doesn't know if the captured this belongs to a struct or an object at invocation time.

Hm- though I suppose if we store the Type of the captured this then we could actually have the debugger properly show the this value instead of just the type-erased void*...

At least in debug mode - and let release be the smaller and faster type-erased version...