I am trying to solve my memory leak issue. I have added a clearEvents function
to my testing modules. My thought is to execute this function prior to
unloading each module. Towards that end I have created a function in the
parentApplication called clearModuleEvents which recursively loops through each
child, and if the "clearEvents " function exists, run it. Then I unload the
module.
Using the profiler when I execute clearModuleEvents, it seems to create another
instance of each display object. I am not sure why. I would appreciate if
someone might tell what might cause that.
Here is the code referenced above;
clearEvents & related code;
protected var dctListeners:Dictionary = new Dictionary();
override public function addEventListener(type:String, listener:Function,
useCapture:Boolean = false, priority:int =
0, useWeakReference:Boolean = true):void { var
key:Object = {type: type, useCapture: useCapture}; if
(dctListeners[key]) { removeEventListener(type,
dctListeners[key], useCapture); dctListeners[key] = null;
} dctListeners[key] = listener;
super.addEventListener(type, listener, useCapture, priority, useWeakReference);
} public function clearEvents():void {
trace('TestMemoryLeak clearEvents'); try {
for (var key:Object in dctListeners) {
removeEventListener(key.type, dctListeners[key], key.useCapture);
dctListeners[key] = null; } } catch
(e:Error) { trace('TestMemoryLeak clearEvents
error='+e.message); } dctListeners = null; }
clearModuleEvents and related code
public function clearModuleEvents(mdlModule:UIComponent):void
{ trace('Starting clearModuleEvents') for(var idx:int =
mdlModule.numChildren - 1; idx >= 0; idx--) { trace('idx=' + idx + '
isContainer(objChild)=' +isContainer(objChild) + '
isContainer(mdlModule.getChildAt(idx))=' +
isContainer(mdlModule.getChildAt(idx))); var objChild:DisplayObject =
mdlModule.getChildAt(idx);
trace('flash.utils.getQualifiedClassName(objChild)=' +
flash.utils.getQualifiedClassName(objChild)); var
childContainer:DisplayObjectContainer = objChild as DisplayObjectContainer;
clearChildEvents(childContainer); childContainer = null; if
("clearEvents" in objChild) { var fncClearEvents:Function =
objChild["clearEvents"]; if (objChild.hasOwnProperty('id'))
trace('Clearing Events in==>>' + objChild['id']); fncClearEvents();
fncClearEvents = null; } if ("dataProvider" in
objChild)objChild["dataProvider"] = null; if ("selectedIndex" in
objChild)objChild["selectedIndex"] = -1; if ("selectedItem" in
objChild)objChild["selectedItem"] = null; if ("value" in
objChild)objChild["value"] = null; if ("text" in
objChild)objChild["text"] = null; objChild = null; } objChild = null;
trace('Finished clearModuleEvents')
}
private function clearChildEvents(dpsContainer:DisplayObjectContainer):void
{ for(var idx:int = dpsContainer.numChildren - 1; idx >= 0; idx--) {
var objChild:DisplayObject = dpsContainer.getChildAt(idx); if
(isContainer(objChild)) { var
childContainer:DisplayObjectContainer = objChild as DisplayObjectContainer;
clearChildEvents(childContainer); childContainer = null;
} if ("killMe" in objChild) { var fncKillMe:Function =
objChild["killMe"]; if (objChild.hasOwnProperty('id')) trace('Killing
Events in==>>' + objChild['id']); fncKillMe(); fncKillMe =
null; } else if ("clearEvents" in objChild) { var
fncClearEvents:Function = objChild["clearEvents"]; if
(objChild.hasOwnProperty('id')) trace('Clearing Events in==>>' +
objChild['id']); fncClearEvents(); fncClearEvents = null;
} if ("dataProvider" in objChild)objChild["dataProvider"] = null;
if ("selectedIndex" in objChild)objChild["selectedIndex"] = -1; if
("selectedItem" in objChild)objChild["selectedItem"] = null;
// if ("value" in objChild)objChild["value"] = null; if ("text" in
objChild)objChild["text"] = null; objChild = null; } objChild = null;
}