first, i must apologize for an error in my code. you cannot make a static
package level var. the code should look like:

package com.example {
    public const API:AppAPI = new AppAPI();
}

Peter, this crazy method i am using does indeed only instantiate the object
only once. i never get a runtime error in accessing the API through the
package level const.

thanks Paul. I made a simple test project and have determined that if i
make it a var instead of const, everything is just fine. Next is to test
whether loading a swf that uses it as a var versus const will allow garbage
collection to pick it up. man, i haven't used a profile in ages.

Henrik, you are right, but i don't think i am wrong about the "flash
player" referencing.

in this special case, the scheduler is built in flash on top of AIR. they
load my swf, then stopAndUnload my swf, show another swf, show an image,
then RELOAD my swf (and by reload i mean loader.loadBytes()), and so on...
so the flash player is never shutting down.

in normal cases, the scheduler is built outside of AIR, so when someone
wants to run our content, the scheduler boots up the flash player only to
play my swf, then shuts it down. even if the scheduler had two back to back
swfs, the flash player instance is always shut down before the new one
opens.

Thanks for your input everyone!


On Fri, Sep 20, 2013 at 10:46 AM, Henrik Andersson <he...@henke37.cjb.net>wrote:

> There is no such thing as calling a constant. And static properties are
> only initialized once.
>
> Your approach only serves to delay the construction of the instance to
> the moment it is first accessed, as opposed to when the runtime decides
> to initialize the constant.
>
> What OP needs is a singleton that can be destructed. The problem here is
> that the constant that is holding a reference to the instance never
> leaves scope and can't be changed not to point at the instance.
>
> The way I would do it would be to either fix the scheduler to correctly
> terminate the flash player or to reuse the flash player instead of
> spawning a new one each time.
>
> In fact, I doubt that OP actually means "flash player", but rather means
> "a random swf file that I load every so often". That would be a prime
> example of where reuse is the correct approach.
>
> Peter Ginneberge skriver:
> > That's not a singleton, as every time you call the public static
> > constant, it creates a new instance.
> >
> > The static var should be private and the class needs an access point
> > method, usually called getInstance();
> >
> >  private static var INSTANCE:AppApi;
> >
> >  public function AppApi():void {
> >   if (INSTANCE != null ) {
> >    throw new Error("Only one instance allowed. " +
> >     "To access the Singleton instance, use getInstance().");   }
> >  }
> >
> >  public static function getInstance():AppApi{
> >   if(!INSTANCE) INSTANCE = new AppApi();
> >   return INSTANCE;
> >  }
> >
> >
> > Then elsewhere in your app you refer to the singleton with:
> > AppApi.getInstance();
> >
> >
>
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>



-- 
Ktu;

The information contained in this message may or may not be privileged
and/or confidential. If you are NOT the intended recipient,
congratulations, you got mail!
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to