//.............................................................................. // // 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 // //.............................................................................. // Perhaps the most well known scenario in which weak pointers are necessary is // the event-subscriber pattern: usually the developer does not want a pointer // held by an event to prevent the subscriber from being collected. This leads // to subtle and hard to track leaks/bugs. // Jancy's solution to this problem is the out-of-the-box support for weak // multicasts. //.............................................................................. class C1 { int m_x = -1; construct () { printf ("C1.construct ()\n"); } destruct () { printf ("C1.destruct ()\n"); } foo () { printf ("C1.foo () { m_x = %d }\n", m_x); } } //.............................................................................. // entry point int main () { printf ("main ()\n"); weak multicast wm (); C1* c = new C1; wm += c.foo; // 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 (); // if the object was collected during the GC run, the corresponding entry // in the weak multicast will be automatically removed when getting the // next snapshot (this is done during the 'mulicast.call' execution) printf ("calling weak multicast...\n"); wm (); return 0; } //..............................................................................