Hey.
Flex blows! Hot and Cold. Its got benefits but its got drawbacks! 6 months into flex and i’m
paying the price of not researching and planning properly, otherwise i may have gone about this
differently. I really hope you can help me. I will credit you on my site. Send you a christmas
card or a hooker (that got your attention - i meant TJ Hooker DVD, well i’ve still got a sense
of humour but its running dry bro) Pls help me. I’m near having a mental breakdown. I’m not
kidding. Great work Adobe!
So lets start with a little history and concept. Its real simple, i thought.
About 8 months ago i discovered papervision. About 6 months ago i found Flex Builder. Merging
the two has become difficult, not in adding papervision to flex but in this way…
I am using Flex Builder Pro/Gumbo 4.0 SDK, targeting FP10.
I am using a simple states based model.
I have my main app called interface. I have managed to load child apps/swfs into the main app
ok using SWFLoader based on enterstate and click events.
The main problem is ram. Using unloadAndStop(true) sorted out the cpu usage problem that unload
was not doing. My loaded swfs get unloaded/killed properly now but the garbage collector does
not free any of the ram that was/is used to render the loaded swf.
I’ve googled this for days now. I’ve tried using modules, i’ve tried using weak listener
references. I’ve tried grant skinners garbage collection hack but cannot get that to work. I’ve
tried using the memory profiler in flex but cannot get that to work either. Don’t ask!
I’ve tried everything in my skill range. I know there are things i should be doing but i’m
kinda all out of trying to get lucky hacking my way to a solution, so i’m asking you.
The problem is when the swf is unloaded the ram used (lets say 30 mb) stays. You don’t have to
be very clever to realise that incrementally as the swfs load and unload without the ram being
taken back that ram keeps going one way making my application redundant - thats a way to win
business! Is this amemory leak or something i’m doing wrong!?!!?
I’ve got a hunch that the problem is in the incoming papervision swf (something to do with the
displayobject bitmap data or the as3 geom used to create the models used in the loaded swfs not
being destroyed or being referenced some way or held in the Flash player, i don’t know why),
but i thought unloadAndStop(true) kills all those things and does a gc. Its probably something
simple that i’m not doing but here goes.
Heres some code (to skilled Flex/AS3 programmers i apologize in advance):
Main App (Interface) Code portion:
<mx:State name=“Car3DScene”
enterState=“initSWF(event);”
exitState=“unloadSWFHandler(event);”>
<mx:AddChild relativeTo="{panel1}" position=“lastChild”>
<mx:Panel id=“mySInnerPanel” width=“100%” height=“100%”
styleName=“myPanelStyleMainContainerInner”
top=“0” right=“0” bottom=“0” left=“0”
paddingTop=“0” paddingRight=“0” paddingBottom=“0” paddingLeft=“0”>
<mx:Spacer width=“100%” height=“5%”/>
<mx:VBox width=“100%” height=“100%”
horizontalAlign=“center” verticalAlign=“top” backgroundAlpha=“0”>
<mx:Panel styleName=“myPanelStyleBoxView2” width=“80%” height=“80%”>
<mx:HBox width=“100%” height=“100%”
borderColor="#777777" borderThickness=“1” borderStyle=“solid”
horizontalAlign=“center” verticalAlign=“middle” backgroundAlpha=“0”>
<mx:HBox borderColor="#1d1d1d" borderThickness=“0” borderStyle=“solid”
backgroundAlpha=“0”>
<mx:Canvas width=“100%” height=“100%”
minHeight=“340” minWidth=“920”
borderColor="#1d1d1d" borderThickness=“1” borderStyle=“solid”
backgroundAlpha=“1” backgroundColor="#000000">
<mx:Image x=“0” y=“0” width=“295” height=“250”
id=“myLogo” source="@Embed(‘assets/images/scene3dMainImage.png’)"
autoLoad=“true” maintainAspectRatio=“false”/>
<mx:Script>
<![CDATA[
import flash.events.*;
import flash.net.URLRequest;
import mx.controls.SWFLoader;
import mx.events.FlexEvent;
import flash.display.Loader;
public var swfContent:MovieClip;
public function initSWF(e:Event):void
{
myLoader.addEventListener(Event.INIT,loadHandler);
myLoader.addEventListener(Event.UNLOAD,unloadSWFHandler);
myLoader.load("assets/swfs/PV3DCanvasSWFFVPortWR.swf");
}
private function loadHandler(e:Event):void
{
swfContent = e.target.content;
}
private function unloadSWFHandler(e:Event):void
{
swfContent.loaderInfo.loader.unloadAndStop(true);
//myLoader.unloadAndStop(true);
}
]]>
</mx:Script>
<mx:SWFLoader x=“295” y=“0”
width=“625” height=“250”
minWidth=“625” minHeight=“250”
maintainAspectRatio=“false”
scaleContent=“true” blendMode=“normal”
id=“myLoader” alpha=“1”/>
…
…
Here is the code of the incoming SWF (Full Code):
<?xml version=“1.0” encoding=“utf-8”?>
<mx:Application xmlns:mx=“http://www.adobe.com/2006/mxml”
preloader=“com.usppro.BasicCustomPreloader”
layout=“absolute”
backgroundGradientColors="[#000000, #000000]"
backgroundGradientAlphas="[1.0, 1.0]"
backgroundColor="#000000" width=“625” height=“250”
applicationComplete=“init3D();”>
<mx:Script>
<![CDATA[
import mx.controls.;
import flash.display.;
import objects.;
import org.papervision3d.cameras.;
import org.papervision3d.materials.;
import org.papervision3d.materials.special.;
import org.papervision3d.objects.;
import org.papervision3d.objects.special.;
import org.papervision3d.render.;
import org.papervision3d.scenes.;
import org.papervision3d.view.;
import org.papervision3d.objects.primitives.;
import org.papervision3d.materials.;
import org.papervision3d.view.;
import org.papervision3d.core.utils.;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.core.geom.renderables.;
private var camera :Camera3D;
private var renderer :BasicRenderEngine;
private var scene :Scene3D;
private var viewport :Viewport3D;
private var universe :DisplayObject3D;
private var c:CarFinal;
[Embed(source="/assets/Car_Black.jpg")]
private var CarTexture:Class;
private function init3D():void
{
Application.application.stage.align = StageAlign.TOP_LEFT;
Application.application.stage.scaleMode = StageScaleMode.NO_SCALE;
Application.application.stage.quality = StageQuality.LOW;
Application.application.stage.frameRate = 30;
viewport = new Viewport3D(625,250,false,false,true,true);
pv3dCanvas.rawChildren.addChild(viewport);
pv3dCanvas.rawChildren.addChild(new Stats());
renderer = new BasicRenderEngine();
scene = new Scene3D();
camera = new Camera3D(universe,18,200);
camera.x = 0;
camera.y = 0;
camera.z = -200;
createObjects();
addEventListeners();
}
private function createObjects():void
{
c = new CarFinal();
c.material = new BitmapMaterial(new CarTexture().bitmapData);
c.x = 0;
c.y = 0;
c.z = 0;
universe = new DisplayObject3D();
universe.addChild(c);
scene.addChild(universe);
universe.x = 200000;
universe.y = -200000;
universe.rotationY = -90;
camera.target = universe;
}
private function addEventListeners():void
{
addEventListener(Event.ENTER_FRAME, loop3D,false,0,true);
}
public function loop3D(e:Event):void
{
camera.target.x -= (((mouseX - (pv3dCanvas.width * 0.5)) * 12) + camera.target.x) / 24;
camera.target.y -= (((mouseY - (pv3dCanvas.height * 0.5)) * 12) + camera.target.y) /
24;
universe.yaw(-0.6);
renderer.renderScene(scene, camera, viewport);
}
]]>
</mx:Script>
<mx:Canvas width=“625” height=“250”
x=“0” y=“0”
blendMode=“normal”
id=“pv3dCanvas”
backgroundColor="#000000" backgroundAlpha=“1”>
</mx:Canvas>
</mx:Application>
In interface i have other states but simply going from my home state to my Car3DScene state and
back and forth loads on every iteration to the 3D state about 30 mb. Soon adds up and i want to
have about 20 3d examples/states.
Can you help?
Noobilicious.