Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Unable to get instance of a facade in multicore  (Read 7188 times)
notjones
Jr. Member
**
Posts: 10


View Profile Email
« on: July 29, 2008, 01:43:42 »

I am using the Multicore version of PureMVC in a good sized Flex project. The application is broken into a number of dynamically loaded modules and each has its own facade. Users select a module by clicking on items in a menu bar.

Most of time things work as expected, but if you click quickly between items in the menu bar or if you switch quickly back and forth between modules, you can cause a problem with the facade get instance code. Symptom of the problem is facade winds up being null in the newly loaded module.

I first thought the problem was related to switching modules before a module had fully loaded and added code to my load routine to ensure that one module is "ready" before I permit a switch to a new module. When switching I call remove core to clear out the PureMVC core for the module going out of scope (and out of memory).

Carefully controlling the swap between modules has reduced the times when we see the problem, but it has not gone away. Modules contain the code creationComplete="moduleInit();" and in the moduleInit() function you find:

public function moduleInit():void
{   
  //Create an instance of MyTeamFacade
 facade = teamFacade.getInstance(getModuleKey());
 facade.startup(this);             
}

The error that occurs points to the facade.startup(this); statement because facade is null.

Anyone have any suggestions on how to figure out what's going on here?


Logged
notjones
Jr. Member
**
Posts: 10


View Profile Email
« Reply #1 on: July 29, 2008, 02:13:58 »

More information on this problem.

Tracing through the static getInstance method in the facade class, I found that the provided key returns an object from the instanceMap, but that object is of type Facade and not classes type. The cast on the return statement fails causing the problem.

The getInstance code will work correctly for six or seven cycles between modules and then it fails.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #2 on: July 29, 2008, 07:38:06 »

Without seeing all of the relevant code, it'd be difficult to tell what exactly is going on. But its conceivable that its a Flex timing thing.


I was talking with another developer today who was using MXML costructed modules and he had a problem like this. He mentioned that he moved the facade startup call to the initialize event instead of creationComplete and the problem went away.

Right now on the day job, (and since the release of MultiCore) I've been using AS3 classes extending ModuleBase rather than MXML Module, and haven't been having any problems.

-=Cliff>
Logged
notjones
Jr. Member
**
Posts: 10


View Profile Email
« Reply #3 on: July 30, 2008, 09:14:20 »

Thanks for the suggestion Cliff. I've now tried several different methods of calling the code that gets the instance of the facade. None of them were successful. With rapid changing between modules I can always make it fail after a number of swaps. What is happening is the instanceMap[key] statement does return an object, but that object is of type Facade instead of an instance of my subtype. The cast then fails.

Finally found a work-around for this issue. In the getInstance method of my subtype I added an optional parameter named "force" and default it to false. When getting the instance of a facade during the initialization of a module I set force to true.

In the getInstance method when force is true, I retrieve the object at instanceMap[key] and cast it to IFacade. If I got an object, I call removeCore(key) on that object and then assign instanceMap[key] to a new instance of my subclass. Using this approach I have not been able to create the break when switching between modules.

The code I'm using is careful to call removeCore on the facade when the module is going out of memory so I'm at a lose to explain why instanceMap[key] finds anything when the module starts up the next time.

Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #4 on: July 30, 2008, 05:35:39 »

Can you reproduce this problem in a petri dish? That is can you send me some code that has this problem without sending anything proprietary?

-=Cliff>
Logged
notjones
Jr. Member
**
Posts: 10


View Profile Email
« Reply #5 on: July 31, 2008, 06:43:46 »

I will try to create some code that reproduces the problem. It will be interesting to see if the problem occurs in simpler code or if it is a issue with the complexity of our application.
Logged
notjones
Jr. Member
**
Posts: 10


View Profile Email
« Reply #6 on: August 05, 2008, 11:29:45 »

Think I found the problem. In org.puremvc.as3.multicore.patterns.facade.Facade instanceMap is defined as a static var of type Array. instanceMap is actually used as an associative array instead of an Array. Changing the definition to Object from Array fixes the problem.

Thanks Cliff for you help in working through this.
Logged
notjones
Jr. Member
**
Posts: 10


View Profile Email
« Reply #7 on: August 05, 2008, 01:52:23 »

Got it wrong again. It is not the definition for instanceMap. It took awhile but the same bug showed up using an Object type for instanceMap. Something is going on with dynamic loading of modules and it is proving very difficult to track down.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #8 on: August 05, 2008, 02:06:02 »

I can at least assure you that there isn't really an inherent problem with loaded modules. I'm currently working with an app that loads LOTS of modules; I have 55 modules loaded in a MultiCore Flex app in another tab of my browser as we speak, all connected with pipes and communicating cozily with each other.

Having looked over your code, I'd have to suggest that you either try to reproduce the problem in a simpler demo, or see where your modules differ from the ones in the demos here on the site from an instantiation and Facade initialization perspective.

Since there aren't any demos up right now that load modules, have a look at this excellent example, which also unloads the module and ensures garbage collection is working properly:
http://www.nutrixinteractive.com/blog/?p=132

-=Cliff>
Logged
pperon
Newbie
*
Posts: 3


View Profile Email
« Reply #9 on: August 05, 2008, 03:16:11 »

I've been having the same issue and have a fairly bare-bones example that may prove to be useful.

[EDIT] Upon further inspection, there were a few things going wrong in my example. I'll re-post when I have something legitimate. (That's what I get for posting after a long day at work  :-\)

-Phil
« Last Edit: August 05, 2008, 07:52:18 by pperon » Logged
Pages: [1]
Print