Re: [Flashcoders] Removing loaded swf...
The last parameter - here set to true - makes the reference to your pageComplete function weak. Meaning if this reference is the only reference left to your removed object that object becomes ready for garbage collection. If I'm not wrong, technically, the function object could be removed before the event even fires for the first time... I've read a lot of times about this best practice, but I'm not really sure if its such a good idea. From Grant Skinner's articles, it seems that the rationale behind this is that since some people will forget to clean up properly after them, let's just use weak refs and let's trust out good luck -- at some point, the GC will do its job, so cross our fingers and hope it doesn't run untimely and screw us. Honestly, I don't think that could be rightfully called a best practice. It rather looks like a source of hard to find bugs. You're basically saying ok, I won't bother to clean up as I should, I'll just use a weak ref and hope for the best, i.e. that the GC won't run before an event I'm listening for is triggered and this best practice backfires at me. I could be wrong, though, but I'd like to hear what others thinks about it. Cheers Juan Pablo Califano 2008/8/15, dr.ache [EMAIL PROTECTED]: Hi Sander. When you add an EventListener you should in 99% set the weak reference flag like this: mLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, pageComplete,false,0,true ); The last parameter - here set to true - makes the reference to your pageComplete function weak. Meaning if this reference is the only reference left to your removed object that object becomes ready for garbage collection. Have a look in the as3 language reference to learn more about it. Mark Sander Schuurman schrieb: Hi cool list! I'm trying do build a generic structure of pages (swf's) loaded in a main swf, and unloading as well ofcourse. Now I'm trying to unload a loaded swf, with the famous Garbage Collection. But I'm not sure I'm doing it the right way. Hope you could point me in the right direction. My code: private function loadPage( qUrl :String ) :void { var mLoader:Loader= new Loader(); var mRequest:URLRequest = new URLRequest( qUrl ); mLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, pageComplete ); mLoader.load( mRequest ); } private function pageComplete( e :Event ) :void { currentPage = e.currentTarget.content; currentPage.addEventListener( Page.PAGE_INIT, initPage ); currentPage.addEventListener( Page.GOTO_PAGE, gotoPage ); addChild(currentPage); } private function gotoPage( e :Event ) :void { var nextPage = currentPage.gotoPage; currentPage.removeEventListener( Page.PAGE_INIT, initPage ); currentPage.removeEventListener( Page.GOTO_PAGE, gotoPage ); var loader:Loader = Loader( e.target ); loader.unload(); loadPage( nextPage ); } I'm not sure about the gotoPage method, if I can make a Loader object of the currentPage and unload it. Is it totally ready for the GC to be deleted? Thnx in advance. Sander ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Removing loaded swf...
PD: On a second thought, I guess I misundertood the issue In this scenario: private function listenerFunc(evt:Event):void { } someDispatcher.addEventListener( someEvent, listenerFunc); listenerFunc has a ref count of 1 before adding it to someDispatcher (and that count will be 2 if I'm not using weak red), so even if I used a weak ref and the GC runs, as long as listenerFunc is still on scope, it won't be collected (because it's ref count would be 1, not 0). Still, removeEventListener should be called explicitly in my opinion, but I guess my previous post was wrong... Cheers Juan Pablo Califano 2008/8/17, Juan Pablo Califano [EMAIL PROTECTED]: The last parameter - here set to true - makes the reference to your pageComplete function weak. Meaning if this reference is the only reference left to your removed object that object becomes ready for garbage collection. If I'm not wrong, technically, the function object could be removed before the event even fires for the first time... I've read a lot of times about this best practice, but I'm not really sure if its such a good idea. From Grant Skinner's articles, it seems that the rationale behind this is that since some people will forget to clean up properly after them, let's just use weak refs and let's trust out good luck -- at some point, the GC will do its job, so cross our fingers and hope it doesn't run untimely and screw us. Honestly, I don't think that could be rightfully called a best practice. It rather looks like a source of hard to find bugs. You're basically saying ok, I won't bother to clean up as I should, I'll just use a weak ref and hope for the best, i.e. that the GC won't run before an event I'm listening for is triggered and this best practice backfires at me. I could be wrong, though, but I'd like to hear what others thinks about it. Cheers Juan Pablo Califano 2008/8/15, dr.ache [EMAIL PROTECTED]: Hi Sander. When you add an EventListener you should in 99% set the weak reference flag like this: mLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, pageComplete,false,0,true ); The last parameter - here set to true - makes the reference to your pageComplete function weak. Meaning if this reference is the only reference left to your removed object that object becomes ready for garbage collection. Have a look in the as3 language reference to learn more about it. Mark Sander Schuurman schrieb: Hi cool list! I'm trying do build a generic structure of pages (swf's) loaded in a main swf, and unloading as well ofcourse. Now I'm trying to unload a loaded swf, with the famous Garbage Collection. But I'm not sure I'm doing it the right way. Hope you could point me in the right direction. My code: private function loadPage( qUrl :String ) :void { var mLoader:Loader= new Loader(); var mRequest:URLRequest = new URLRequest( qUrl ); mLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, pageComplete ); mLoader.load( mRequest ); } private function pageComplete( e :Event ) :void { currentPage = e.currentTarget.content; currentPage.addEventListener( Page.PAGE_INIT, initPage ); currentPage.addEventListener( Page.GOTO_PAGE, gotoPage ); addChild(currentPage); } private function gotoPage( e :Event ) :void { var nextPage = currentPage.gotoPage; currentPage.removeEventListener( Page.PAGE_INIT, initPage ); currentPage.removeEventListener( Page.GOTO_PAGE, gotoPage ); var loader:Loader = Loader( e.target ); loader.unload(); loadPage( nextPage ); } I'm not sure about the gotoPage method, if I can make a Loader object of the currentPage and unload it. Is it totally ready for the GC to be deleted? Thnx in advance. Sander ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Removing loaded swf...
Hi cool list! I'm trying do build a generic structure of pages (swf's) loaded in a main swf, and unloading as well ofcourse. Now I'm trying to unload a loaded swf, with the famous Garbage Collection. But I'm not sure I'm doing it the right way. Hope you could point me in the right direction. My code: private function loadPage( qUrl :String ) :void { var mLoader:Loader= new Loader(); var mRequest:URLRequest = new URLRequest( qUrl ); mLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, pageComplete ); mLoader.load( mRequest ); } private function pageComplete( e :Event ) :void { currentPage = e.currentTarget.content; currentPage.addEventListener( Page.PAGE_INIT, initPage ); currentPage.addEventListener( Page.GOTO_PAGE, gotoPage ); addChild(currentPage); } private function gotoPage( e :Event ) :void { var nextPage = currentPage.gotoPage; currentPage.removeEventListener( Page.PAGE_INIT, initPage ); currentPage.removeEventListener( Page.GOTO_PAGE, gotoPage ); var loader:Loader = Loader( e.target ); loader.unload(); loadPage( nextPage ); } I'm not sure about the gotoPage method, if I can make a Loader object of the currentPage and unload it. Is it totally ready for the GC to be deleted? Thnx in advance. Sander ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Removing loaded swf...
In gotoPage(), try this: var loader:Loader = Loader(currentPage.parent); instead of: var loader:Loader = Loader(e.target); // e.target refers to currentPage -- Eduardo Omine http://blog.omine.net/ http://www.omine.net/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Removing loaded swf...
Hmm, Loader.unload() only removes the content from the loader object. It might be a good idea to remove it from the container you are adding it to. Make sure currentPage has no listeners to objects outside of its own scope, or objects inside of its scope listening to anything out of its scope, or objects outside of its scope referencing anything inside of its scope, and all streams closed when you unload. Always clean up timers and enterframe listeners from the swf being loaded. It is a good idea to have a deinit() like method that you call for clean up. I think you're okay if you're cleanly in general :) Here is a nice article about System.gc(), it might be helpful: http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/ And if you haven't already, check this out: http://www.gskinner.com/blog/archives/2008/04/failure_to_unlo.html H On Fri, Aug 15, 2008 at 7:41 AM, Sander Schuurman [EMAIL PROTECTED] wrote: Hi cool list! I'm trying do build a generic structure of pages (swf's) loaded in a main swf, and unloading as well ofcourse. Now I'm trying to unload a loaded swf, with the famous Garbage Collection. But I'm not sure I'm doing it the right way. Hope you could point me in the right direction. My code: private function loadPage( qUrl :String ) :void { var mLoader:Loader= new Loader(); var mRequest:URLRequest = new URLRequest( qUrl ); mLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, pageComplete ); mLoader.load( mRequest ); } private function pageComplete( e :Event ) :void { currentPage = e.currentTarget.content; currentPage.addEventListener( Page.PAGE_INIT, initPage ); currentPage.addEventListener( Page.GOTO_PAGE, gotoPage ); addChild(currentPage); } private function gotoPage( e :Event ) :void { var nextPage = currentPage.gotoPage; currentPage.removeEventListener( Page.PAGE_INIT, initPage ); currentPage.removeEventListener( Page.GOTO_PAGE, gotoPage ); var loader:Loader = Loader( e.target ); loader.unload(); loadPage( nextPage ); } I'm not sure about the gotoPage method, if I can make a Loader object of the currentPage and unload it. Is it totally ready for the GC to be deleted? Thnx in advance. Sander ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Removing loaded swf...
Hi Sander. When you add an EventListener you should in 99% set the weak reference flag like this: mLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, pageComplete,false,0,true ); The last parameter - here set to true - makes the reference to your pageComplete function weak. Meaning if this reference is the only reference left to your removed object that object becomes ready for garbage collection. Have a look in the as3 language reference to learn more about it. Mark Sander Schuurman schrieb: Hi cool list! I'm trying do build a generic structure of pages (swf's) loaded in a main swf, and unloading as well ofcourse. Now I'm trying to unload a loaded swf, with the famous Garbage Collection. But I'm not sure I'm doing it the right way. Hope you could point me in the right direction. My code: private function loadPage( qUrl :String ) :void { var mLoader:Loader= new Loader(); var mRequest:URLRequest = new URLRequest( qUrl ); mLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, pageComplete ); mLoader.load( mRequest ); } private function pageComplete( e :Event ) :void { currentPage = e.currentTarget.content; currentPage.addEventListener( Page.PAGE_INIT, initPage ); currentPage.addEventListener( Page.GOTO_PAGE, gotoPage ); addChild(currentPage); } private function gotoPage( e :Event ) :void { var nextPage = currentPage.gotoPage; currentPage.removeEventListener( Page.PAGE_INIT, initPage ); currentPage.removeEventListener( Page.GOTO_PAGE, gotoPage ); var loader:Loader = Loader( e.target ); loader.unload(); loadPage( nextPage ); } I'm not sure about the gotoPage method, if I can make a Loader object of the currentPage and unload it. Is it totally ready for the GC to be deleted? Thnx in advance. Sander ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Removing loaded swf...
Hi, I thought the point of Grant's article was that it's hopelessly bugged in player 9 even if you are an academic?:) greetz JC On Fri, Aug 15, 2008 at 3:47 PM, Eduardo Omine [EMAIL PROTECTED]wrote: In gotoPage(), try this: var loader:Loader = Loader(currentPage.parent); instead of: var loader:Loader = Loader(e.target); // e.target refers to currentPage -- Eduardo Omine http://blog.omine.net/ http://www.omine.net/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Removing loaded swf...
It is a bit like that isn't it :) H On Fri, Aug 15, 2008 at 9:13 AM, Hans Wichman [EMAIL PROTECTED] wrote: Hi, I thought the point of Grant's article was that it's hopelessly bugged in player 9 even if you are an academic?:) greetz JC On Fri, Aug 15, 2008 at 3:47 PM, Eduardo Omine [EMAIL PROTECTED] wrote: In gotoPage(), try this: var loader:Loader = Loader(currentPage.parent); instead of: var loader:Loader = Loader(e.target); // e.target refers to currentPage -- Eduardo Omine http://blog.omine.net/ http://www.omine.net/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders