2012-06-08 37 views
0

我一直在寻找谷歌,但我找不到一个好的答案。 我正在使用Flex 4并使用模块构建一个空气应用程序(因为这是一个大项目,所以会有很多模块)。 我设法加载模块在由popupmanager调用的titlewindow中,但是当titlewindow被关闭时,模块不会被卸载(garbaged) - 我在flasbuilder中使用profiler来检查这个模块。Flex 4 AIR APP卸载模块的正确方法

这是我的代码,我需要知道如果我在项目中使用模块之前正在使用模块进行正确的方向。

感谢所有

Main APP: MXML 

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx" 
        xmlns:tblusersservice="services.tblusersservice.*" 
        xmlns:valueObjects="valueObjects.*" 
        xmlns:tbluserservice="services.tbluserservice.*" 
        width="100%" height="100%" applicationComplete="checkForUpdate()" preinitialize="nativeWindow.maximize();" currentState="login"> 

<fx:Script source="includes/_loadtracker.as"/> 



<s:Panel id="panelmain" includeIn="mainmenu" left="5" width="100%" height="100%" resizeEffect="Resize" title="Main menu"> 

    <s:Image id="companymenu" right="15" top="130" width="118" height="93" buttonMode="true" 
      click="loadmodule('mod_company', 'Company Information', 931, 446);" source="assets/company.png" useHandCursor="true"/> 


</s:Panel> 



</s:WindowedApplication> 




_loadtracker.as: 

// ActionScript file 
import flash.filesystem.*; 
import flash.events.ErrorEvent; 
import flash.events.MouseEvent; 
import flash.events.Event; 
import flash.display.*; 
import air.update.ApplicationUpdaterUI; 
import air.update.events.UpdateEvent; 
import mx.controls.Alert;  
import mx.managers.PopUpManager; 
import mx.rpc.events.ResultEvent; 
import spark.components.TitleWindow; 
import valueObjects.*; 
import flash.desktop.NativeApplication; 



// Open the pop-up window. 
private function loadmodule(modname:String, modtitle:String, modwidth:int, modheight:int):void { 
// Create a non-modal TitleWindow container. 
settings.moduletoload = modname; 
var titleWindow:TitleWindow= 
    PopUpManager.createPopUp(this, showmodules, true) as TitleWindow; 
titleWindow.title = modtitle; 
titleWindow.width = modwidth; 
titleWindow.height = modheight + 35; 
    PopUpManager.centerPopUp(titleWindow); 
} 





showmodules.mxml 

<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="400" creationComplete="initModule()" close="handleCloseEvent()"> 

<fx:Script> 
    <![CDATA[ 

     import mx.controls.Alert; 
     import mx.core.IVisualElement; 
     import mx.events.ModuleEvent; 
     import mx.managers.PopUpManager; 
     import mx.modules.IModuleInfo; 
     import mx.modules.ModuleManager; 
     import mx.rpc.events.ResultEvent; 
     import services.tbluserservice.*; 

     public var info:IModuleInfo; 
     public var modclosed:Boolean = false; 

     private function initModule():void { 
      this.addEventListener("foobar", handleCloseEventmodule); 

      info = ModuleManager.getModule("/modules/"+settings.moduletoload+".swf"); 
      info.addEventListener(ModuleEvent.READY, modEventHandler);   

      info.load(null, null, null, moduleFactory); 
     } 

     /* Add an instance of the module's class to the display list. */   
     private function modEventHandler(e:ModuleEvent):void { 

      this.addElement(info.factory.create() as IVisualElement); 
     } 

     // Handle the close button and Cancel button. 
     public function handleCloseEvent():void { 

       PopUpManager.removePopUp(this); 
       info.unload(); 
       info.release(); 
       info = null; 

     } 

    ]]> 
</fx:Script> 

<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
</s:TitleWindow> 




mod_company.mxml 

<s:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" 
     xmlns:tblcompanyservice="services.tblcompanyservice.*" 
     xmlns:valueObjects="valueObjects.*" 
     width="931" height="446" 
     creationComplete="LoadData()"> 
<fx:Script> 
    <![CDATA[ 
     import mx.events.FlexEvent; 

     protected function dataGrid_creationCompleteHandler(event:FlexEvent):void 
     { 
      getAllTblcompanyResult.token = tblcompanyService.getAllTblcompany(); 
     } 

    ]]> 
</fx:Script> 

<fx:Script source="../includes/_company.as"/> 

<fx:Declarations> 
    <tblcompanyservice:TblcompanyService id="tblcompanyService" 
             fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" 
             showBusyCursor="true"/> 
    <s:CallResponder id="getTblcompanyByIDResult" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" 
        result="tblcompany = getTblcompanyByIDResult.lastResult as Tblcompany"/> 
    <valueObjects:Tblcompany id="tblcompany"/> 
    <s:CallResponder id="updateTblcompanyResult"/> 
    <s:CallResponder id="getAllTblcompanyResult"/> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
<s:Label x="81" y="41" text="COMPANY NAME"/> 
<s:Label x="81" y="71" text="ADDRESS"/> 
<s:Label x="83" y="131" text="CITY"/> 
<s:Label x="83" y="161" text="STATE"/> 
<s:Label x="83" y="191" text="ZIP"/> 
<s:Label x="83" y="221" text="COUNTRY"/> 
<s:Label x="582" y="41" text="TELEPHONE"/> 
<s:Label x="582" y="71" text="FAX"/> 
<s:Label x="582" y="102" text="WATTS"/> 
<s:Label x="83" y="276" text="OWNER"/> 
<s:Label x="83" y="324" text="LOGO PATH"/> 
<s:TextInput id="fNameTextInput" x="185" y="32" width="323" text="{tblcompany.fName}"/> 
<s:TextInput id="faddressTextInput" x="185" y="62" width="256" text="{tblcompany.faddress}"/> 
<s:TextInput id="faddress2TextInput" x="185" y="92" width="256" text="{tblcompany.faddress2}"/> 
<s:TextInput id="fcityTextInput" x="185" y="122" width="256" text="{tblcompany.fcity}" textAlign="left"/> 
<s:TextInput id="fstateTextInput" x="185" y="152" width="256" text="{tblcompany.fstate}"/> 
<s:TextInput id="fzipTextInput" x="185" y="182" width="81" text="{tblcompany.fzip}"/> 
<s:TextInput id="fcountryTextInput" x="185" y="212" width="256" text="{tblcompany.fcountry}"/> 
<s:TextInput id="ftelTextInput" x="701" y="32" text="{tblcompany.ftel}"/> 
<s:TextInput id="ffaxTextInput" x="701" y="62" text="{tblcompany.ffax}"/> 
<s:TextInput id="fwattsTextInput" x="701" y="92" text="{tblcompany.fwatts}"/> 
<s:TextInput id="fownerTextInput" x="185" y="266" width="418" text="{tblcompany.fowner}"/> 
<s:TextInput id="flogopathTextInput" x="185" y="314" width="644" text="{tblcompany.flogopath}"/> 
<s:TextInput id="fidTextInput" x="224" y="379" text="{tblcompany.fid}" visible="false"/> 
<s:Button id="button" x="79" y="379" label="Save" click="button_clickHandler(event)"/> 
<s:DataGrid id="dataGrid" x="158" y="242" 
      creationComplete="dataGrid_creationCompleteHandler(event)" requestedRowCount="4"> 
    <s:columns> 
     <s:ArrayList> 
      <s:GridColumn dataField="fid" headerText="fid"></s:GridColumn> 
      <s:GridColumn dataField="fName" headerText="fName"></s:GridColumn> 
      <s:GridColumn dataField="fowner" headerText="fowner"></s:GridColumn> 
      <s:GridColumn dataField="faddress" headerText="faddress"></s:GridColumn> 
      <s:GridColumn dataField="faddress2" headerText="faddress2"></s:GridColumn> 
      <s:GridColumn dataField="fcity" headerText="fcity"></s:GridColumn> 
      <s:GridColumn dataField="fzip" headerText="fzip"></s:GridColumn> 
      <s:GridColumn dataField="fstate" headerText="fstate"></s:GridColumn> 
      <s:GridColumn dataField="fcountry" headerText="fcountry"></s:GridColumn> 
      <s:GridColumn dataField="ftel" headerText="ftel"></s:GridColumn> 
      <s:GridColumn dataField="ffax" headerText="ffax"></s:GridColumn> 
      <s:GridColumn dataField="fwatts" headerText="fwatts"></s:GridColumn> 
      <s:GridColumn dataField="flogopath" headerText="flogopath"></s:GridColumn> 
      <s:GridColumn dataField="femail" headerText="femail"></s:GridColumn> 
     </s:ArrayList> 
    </s:columns> 
    <s:typicalItem> 
     <fx:Object faddress="faddress1" faddress2="faddress21" fcity="fcity1" 
        fcountry="fcountry1" femail="femail1" ffax="ffax1" fid="fid1" 
        flogopath="flogopath1" fName="fName1" fowner="fowner1" fstate="fstate1" 
        ftel="ftel1" fwatts="fwatts1" fzip="fzip1"></fx:Object> 
    </s:typicalItem> 
    <s:AsyncListView list="{getAllTblcompanyResult.lastResult}"/> 
</s:DataGrid> 
</s:Module> 




_company.as 


// ActionScript file 
//import flash.desktop.NativeApplication; 
import flash.events.MouseEvent; 
import flash.events.Event; 
import mx.controls.Alert; 
//import mx.core.Application; 
//import mx.core.mx_internal; 
import services.tblcompanyservice.*; 
import valueObjects.*; 

protected function LoadData():void { 
getTblcompanyByIDResult.token = tblcompanyService.getTblcompanyByID(parseInt("1")); 
} 

protected function button_clickHandler(event:MouseEvent):void { 
tblcompany.fid = parseInt(fidTextInput.text); 

tblcompany.fName = fNameTextInput.text; 
tblcompany.fowner = fownerTextInput.text; 
tblcompany.faddress = faddressTextInput.text; 
tblcompany.faddress2 = faddress2TextInput.text; 
tblcompany.fcity = fcityTextInput.text; 
tblcompany.fzip = fzipTextInput.text; 
tblcompany.fstate = fstateTextInput.text; 
tblcompany.fcountry = fcountryTextInput.text; 
tblcompany.ftel = ftelTextInput.text; 
tblcompany.ffax = ffaxTextInput.text; 
tblcompany.fwatts = fwattsTextInput.text; 
tblcompany.flogopath = flogopathTextInput.text; 
tblcompany.femail = ""; 
updateTblcompanyResult.token = tblcompanyService.updateTblcompany(tblcompany); 
//Alert.show("Modifications saved"); 
//this.dispatchEvent(new Event("foobar", true)); 
} 

回答

0

卸载Flex模块具有historically been problematic。 Flex 4和我认为4.5开始削弱潜在问题,让我们的生活更轻松。

当您的主应用程序中的某些内容维护对模块中某个对象的引用时,它将阻止您的模块卸载。有很多方法可以发生。由于Flex的工作方式,Flex可能仍然会导致一些问题。但主要的麻烦已经被缓解了(尽管我确定你使用Flex 4.5)。

现在想想这件事你绝对明智。不是选择使用模块,而是确保它们正在卸载。

我链接到的文章是来自Flex SDK开发人员的旧文章,而其中一些问题可能不再存在,这些概念应该是有启发性的。

编辑

在进一步审查,仅是跳出我的事情之一似乎值得看的:

  • 你的模块中的脚本标签可以有风格可以在同一个问题介绍(也可能是一个红鲱鱼)。这个想法是,使用这个脚本的第一个类可能与Flex内部的脚本永久关联。如果该类是一个模块,它将永远不会卸载。作为测试,您也可以尝试在WindowedApplication类中声明脚本标记(即使它未被使用)。

我想过的另一件事是在您的视图中可绑定变量。但在进一步的审查中,我看不到这些可能导致内存泄漏。

+0

我使用flex 4.6 actualy。我也是新来flex,你是否看到我的代码中可能导致这种情况的东西?谢谢 – gfbaggio

+0

Doh,我怎么能忘记4.6(在度假)!也许模块中有些东西可以查看(编辑我的答案)。 –

+0

我删除了脚本标记并将所有AS包含在相同的代码中。进行了如下更改 – gfbaggio

0

我认为问题在于您每次创建一个新的TitleWindow,并且TitleWindow将事件侦听器添加到通过模块加载器和never removes it加载的模块中。从理论上讲,PopupManager.removePopup也应该每次取消引用TitleWindow,但是,老实说,将这个功能编写为全局/静态类的人可能不能被信任遵守其他地方的良好实践,因此您应该可以使用F3查看PopupManager上的代码,并确保它删除它添加的任何事件侦听器,并将对标题窗口的引用归零。由于它是一个静态类,一旦有事与它交谈,如果它没有正确解引用它们,它们将坚持应用程序的生命。

接下来,即使您已经加载了一个模块,也是手动创建了模块的实例。你需要做一些事情(除去你的事件监听器)试图放开加载器,但你永远不会释放手动创建的实例。为了简化一些事情,可以尝试添加已加载的模块,而不是创建额外的模块。

另一个建议是而不是使用视图元素来代替数据存储。所以,而不是绑定到VO,当你得到它从你的领域填充。当有任何变化时,根据VO的值更新VO。这会导致更多的代码,但首先,您总是知道您的数据是最新的。另一方面,如果需要,数据可以独立于视图而传播。但是这是一件可以帮助你消除绑定将VO保存在内存中的可能性。

还有一些事情会在代码周围产生一丝淡淡的稗味,这也可能是问题的根源。例如,你的标题窗口子类似乎有一个设置的参考,即使你还没有给出它。这表明设置实际上是一个静态类,并且您忽略了类应以大写字母开头的命名约定。更重要的是,如果你有像这样的后台渠道进行沟通,那么就没有办法看你的代码,并确定你没有发生什么事情,在某种程度上你已经给出了设置参考它所持有的东西到。

你也有什么似乎是一个未声明的变量,modulefactory,但我认为这可能是一个命名错误的类,它与TitleWindow生活在同一个包中(所以没有import语句)。快速的建议:如果你想在论坛上获得帮助,遵循命名规则将使希望帮助你解开你所做的事情的人更容易。在这种情况下,可能会有一些地方出现“非法”功能,妨碍了最佳实践OOP通信,并且由于您命名的方式而不容易识别。但最终,我认为如果你只下载到模块的一个副本并删除了事件监听器,那么你应该可以放弃你的模块,它应该让你释放VO。

+0

我试图在我的主应用程序中包含所有的remoteobject,而不是将它们放在模块中,但是我还没有找到任何有关tomdo的例子。你能帮助我吗?谢谢 – gfbaggio

+0

这里有几篇文章讨论了从使用依赖注入框架来处理这个问题的VO进行重构,这是一个类似的概念:http://www.developria.com/2010/06/robotlegs -for-framework-beginn.html http://www.developria.com/2010/05/refactoring-with-mate.html。以下是原始http://flexdiary.blogspot.com/2009/01/lazy-loading-tree-example-file-posted.html,其中服务代码至少是模型的一部分,而不是视图。 –