//.............................................................................. // // This file is part of the Jancy toolkit. // // Jancy is distributed under the MIT license. // For details see accompanying license.txt file, // the public copy of which is also available at: // http://tibbo.com/downloads/archive/jancy/license.txt // //.............................................................................. // Just like with function pointers, property pointers can be 'weak', meaning // that they do not retain 'this' argument captured in the closure from // being collected by the GC. //.............................................................................. class C1 { construct () { printf ("C1.construct ()\n"); } destruct () { printf ("C1.destruct ()\n"); } int autoget property m_foo; m_foo.set (int x) { printf ("C1.m_foo.set (%d)\n", x); m_value = x; } } //.............................................................................. // entry point int main () { printf ("main ()\n"); C1* c = new C1; int property weak* wp = &c.m_foo; // weak property pointers cannot be used to get or set a property // *wp = 10; // <-- error // if we lose the strong pointer then the object will get destructed on the // next GC run // c = null; printf ("gc run...\n"); sys.collectGarbage (); // attempt to recover the strong pointer int property* p = wp; if (p) { printf ("object survived gc run\n"); *p = 100; } else { printf ("object was collected during gc run\n"); } return 0; } //..............................................................................