Note that there are some explanatory texts on larger screens.

plurals
  1. POFlash Player: Get reference count for variable
    primarykey
    data
    text
    <p>I'm looking to build a library that needs to be very careful about memory management. Basically, I <em>have</em> to create a static factory to "disperse" instances of my tool to requesting objects. (I don't have a choice in this matter, I really do have to use a singleton) We'll call that class <code>FooFactory</code>. <code>FooFactory</code> defines a single method, <code>getFoo(key:String):Foo</code>. </p> <p><code>getFoo</code> looks in a private static <code>flash.utils.Dictionary</code> object for the appropriate <code>Foo</code> instance, and either lazy-instantiates it, or simply returns it. In any case, <code>FooFactory</code> <em>MUST</em> keep a reference to each <code>Foo</code> instance created, so all <code>Foo</code> instances can be updated by <code>FooFactory</code> using a method called <code>updateFoos():void</code>. </p> <p>Here is some pseudo-code of what I'm talking about:</p> <pre><code>public class FooFactory { private static const foos:Dictionary = new Dictionary(true); //use weak keys for gc public static function getFoo(key:String):Foo { //search for the specified instance in the 'foos' dictionary if (foos[key] != null &amp;&amp; foos[key] != undefined) { return foos[key]; } else { //create foo if it doesn't exist. var foo:Foo = new Foo(key); foos[key] = foo; return foo; } } public static function updateFoos():void { for (var key:String in foos) { if (foos[key] != null &amp;&amp; foos[key] != undefined) { Foo(foos[key]).dispatchEvent(new Event("update")); } } } } </code></pre> <p>The actual function and identity of <code>Foo</code> isn't too important. </p> <p>What <strong>IS</strong> important is garbage collection in this situation. I created something similar to the above example in the past and had incredible garbage collection issues. (I <em>did</em> use an array rather than a dictionary, which could be part of the problem.) What would happen is that, in my Flex application, modules would never unload, since instances had a reference to a <code>Foo</code> instance which was referenced by the <code>FooFactory</code>, like so: (again, pseudocode)</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;s:Group&gt; &lt;fx:Script&gt; &lt;![CDATA[ private static const foo:Foo = FooFactory.getFoo('myfoo'); ]]&gt; &lt;/fx:Script&gt; &lt;/s:Group&gt; </code></pre> <p>What I want to know are the two following things:</p> <ol> <li>Is the pseudo-code above "garbage-collector safe?" IE: Will my modules unload properly and will instances of the <code>Group</code> subclass above get garbage collected? </li> <li>Is there a way in Flash Player (even in the debug player if need be) that can assist me in counting references so I can test if things are getting garbage collected or not? </li> </ol> <p>I'm aware of the <code>flash.sampler</code> API, but I am not sure as to how to use it to count references. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload