Unloading a Module Containing Papervision 3D Scenes and Objects
During my last project I have been facing a problem related to loading and unloading modules that includes scenes made using the library Papervision 3D. At First, when I check the status of memory used by my application, I see that, unloading modules that contains such scenes using module.unload() didn’t decrease memory usage at all.
I tried first to find out why I was unable to unload the module while I thought that using the function unload() will automatically destroy and remove everything related to that module. During my searches I found some very interesting articles, the best that helped me out is the one published by Alex Harui here. The problem I got is that these modules contains Papervision 3D Scenes and Objects that the function module.unload() can’t automatically delete. To be able to do it, I needed first to clean the Papervision 3D scenes, remove the objects holding these scenes and then, when all is done, unload the module.
After understanding the importance of cleaning out the Papervision 3D related objects, I searched the net to find out what can be the best way to do it and I have been inspired by the demo made by ultra-web here about destroyable PV3D Objects. I found it interesting but I didn’t like too much the Idea of tweaking PV3D classes while it can be problematic when I may want to update the Papervision 3D Library later.
My last reference to sort out the problem was the book of Paul Tondeur and Jeff Winder called Papervision 3D Essentials first published in September 2009 by Packt Publishing LTD. In Chapter 13, Optimizing Performance, page 384. he gave the best way, I think, to remove such problematic Papervision related objects which is :
- to remove primitives (plans, cubes, spheres, etc.) from the scene, using removeChild() Method, exemple:
myScene.removeChild(myPapervisionObject);
- then, destroy their materials
myPapervisionObject.material.destroy();
- then, set them to null
myPapervisionObject = null;
That was the best way to unload the Papervision 3D Objects. I just ensured then to remove all objects from the module and used, for that, an Event dispatched when all operations done to main application informing it that it can unload the module.
