Recursively turning a stack into a linked list - recursion

so I have been working on a programming assignment that involves taking a stack implementation of size ~13,000 and turning it into a linked list. The guide is basically that the stack was filled by sequentially scanning a linked list (IE tail would be the top of the stack), and you want to re create the linked list using the stack. The trick is you have to do it using a recursive method. The only methods in this stack class are pop (returns and removes the top element), and isEmpty(tells if the stack is empty). I have code that gets the job done, however it requires increasing the java stack size (otherwise I get StackOverflowError), which I feel like that isn't allowed.
That being said does anyone know a way I could possibly get this to work without increasing the java stack size.
The stack is a static field I have labeled S. Head is what should be the first node in the linked list, and steper is simply a node to be used to create every other step.
Here is the code I currently have:
public static void stackToList()
{
int x = 0;
if(S.isEmpty())
{
return;
}
x = S.pop();
stackToList();
if (head == null)
{
head = new ListNode(x, null);
steper = head;
}
else
{
steper.next = new ListNode(x, null);
steper = steper.next;
}
}
Thank you ahead of time for any help.

It is happening because you are keeping an entire list of function calls in memory stack. You start creating your linked list only after you reach to the bottom of the stack thus keeping all the previous calls to stackList waiting to be over.
You need to start creating your linked list with the first pop of stack.
A simple & non tested (not worked in Java in a very long time now) function may look like:
public static ListNode stackToList(ListNode head) {
if(S.isEmpty())
return head;
int stackValue = S.pop();
ListNode node = ListNode(stackValue, null);
node.next(head);
return stackToList(node);
}
And you call it like:
ListNode head = stackToList(null)
HTH
EDIT: Now that I posted it, I realized that my code has potentially the same issue as yours, because I remembered Java doesn't support tail-call optimization.

It's not entirely clear from your question if you're using java.util.LinkedList and java.util.Stack but since you didn't provide the code for these objects I will be using those in my example solution below:
public static void main(String[] args) {
//Create a stack
Stack<Integer> stack = new Stack<Integer>();
stack.push(0);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
stack.push(9);
//Create a list to hold your stack elements
LinkedList<Integer> linkedList = new LinkedList<Integer>();
//Call the conversion method, which modifies both the stack and the list
convertStackToLinkedList(stack, linkedList);
//print the results
System.out.println("linkedList: "+linkedList);
}
public static void convertStackToLinkedList(Stack<Integer> stack, LinkedList<Integer> linkedList){
int topStackElement = stack.pop();
linkedList.add(0,topStackElement);
if(!stack.isEmpty())
convertStackToLinkedList(stack, linkedList);
}
I suspect you may not be using java.util.LinkedList since your code is attempting to modify the internals of the list. So, you would simply need to implement a method similar to that of the add(int index, E element) method in the java.util.LinkedList class and then use it in your recursion. I assume you can do this if you have access to the internals of the list.
EDIT:
I forgot to mention that I agree with the answer by Harsh Gupta in that the reason you're seeing StackOverflowError is that you're waiting until you reach the end of your recursion to modify your list. In some recursion you have to wait until the end but if you don't have to wait don't do it.

Related

TraversalEngine abstract class utilisation

Firstly, i am sorry but i don't speak english very well. Secondly, i have a problem with nodes which are put in a gridpane. In fact, if the focus is taken by the first one wich is located on the top left side, when i push the tab key, the focus is not taken by the other which is located on the right.
People ask me to use the traversalEngine abstract class in order to solve this problem. Nevertheless, when i try to implement an engine object, it doesn't work if i put the parameters which are shown everywhere on the web:
TraversalEngine engine = new TraversalEngine(gridPane, false) {
It ask me to remove the parameters. If i do it, i don't have access to the trav method. In fact, it is the getRoot method which appears and can be implemented :
TraversalEngine engine = new TraversalEngine() {
#Override
protected Parent getRoot() {
// TODO Auto-generated method stub
return null;
}
}
Is there something which can be make in order to solve this problem ?
Thanks you for your help
Vinz
The traversal order for focusing nodes in a parent is the order in which they occur in the child list. Assuming every child contains at most one focusable node you could simply add the children line by line or reorder the children.
This could be done programmatically of course, but adding the children in the correct order in the first place would be more efficient...
public static int getColumnIndex(Node n) {
Integer i = GridPane.getColumnIndex(n);
return i == null ? 0 : i;
}
public static int getRowIndex(Node n) {
Integer i = GridPane.getRowIndex(n);
return i == null ? 0 : i;
}
grid.getChildren().sort(Comparator.comparingInt(ContainingClass::getRowIndex).thenComparingInt(ContainingClass::getColumnIndex));

Integrating smart pointers with legacy code raw pointers

I have a situation, where I have existing code that works with raw pointers, and I'm not permitted to smart-pointer-ify it. However, I am permitted to use smart pointers in any new code I develop.
For example.
I have an existing function like:
void processContent()
{
ContentObject * myContent = new ContentObject();
newFunction(myContent);
}
void newFunction(ContentObject * content)
{
// myVector is just a std::vector<ContentObject*>, defined elsewhere
myVector.push_back(content);
}
void doSomethingWithContent()
{
// There is some logic here, but ultimately based on this logic I want to remove entries, and free the memory they point to.
myVector.pop_back();
}
I have control over the content of "newFunction" and "doSomethingWithContent". But the argument passed into newFunction is fixed. Obviously I could manually delete the pointer in myVetor, before popping it, but I wondered if I can implement smart pointers here so that it happens "automatically" for me?
Can I take a raw pointer passed into a function, and turn it into a unique_ptr, then add this to a container, and have it delete the memory when it's popped from the container?
Thanks
Joey
Assume that you can define your myVector as the following:
std::vector<std::shared_ptr<ContentObject>> myVector;
In that case you can switch on smart pointers in your code and myVector will keep all your objects as you expected:
void newFunction(ContentObject * content)
{
myVector.push_back(std::shared_ptr<ContentObject>(content));
}
void doSomethingWithContent()
{
// There is some logic here, but ultimately based on this logic I want to remove entries, and free the memory they point to.
myVector.pop_back();
}

AspectJ - Why cflow leads to an infinite recursion when it is not combined with an && (aspectJ intersection)?

I have a question about cflow or cflowbelow in AspectJ.
cflow(Pointcut)
Picks out all join points in the control flow of the
join points picked out by the pointcut, including pointcut's join
points themselves.
cflowbelow(Pointcut)
Picks out all join points in the control flow
below the join points picked out by the pointcut.
For both, I could find only the definition and that:
When defining pointcut using cflow or cflowbelow, we need to ensure
that the pointcut does not capture the calls that are made from the
same aspect, otherwise it will invoke recursive method calls and we
will get StackOverflowError.
This happens because cflow() will also
capture the method calls from the aspect itself and try to apply the
advice to it. This can be avoided by using within() construct.
within() takes a type(class or interface) name as argument and
captures all join points the are defined in that type.
But no explanation about how actually cflow() or cflowbelow() lead to an infinite recursion when they are used without within or together with an && expression like:
pointcut aPointcut(): execution(void Test.foo()) && !cflowbelow(execution(void Test.foo()));
Which will match e.g. the first execution of Test.foo() only ignoring any bubbling if inside Test.foo() another call to Test.foo() is made or a call to a foo() method of a class which extends Test is made.
My question is: why cflow actually leads to an infinite recursion when it is used e.g. without within? How does the weaving with cflow happens so that it leads to such a recursion?
A simple example:
Driver application:
package de.scrum_master.app;
public class Application {
public static void sayHelloTo(String name) {
System.out.println("Hello " + name + "!");
}
public static void main(String[] args) {
sayHelloTo("world");
}
}
Aspect:
package de.scrum_master.aspect;
import de.scrum_master.app.Application;
public aspect CflowRecursionDemo {
// Attention, StackOverflowError!
/*
before() : cflow(execution(* Application.sayHelloTo(..))) {
System.out.println(thisJoinPoint);
}
*/
before() : !within(CflowRecursionDemo) && cflow(execution(* Application.sayHelloTo(..))) {
System.out.println(thisJoinPoint);
}
}
The commented-out advice leads to a StackOverflowError while the active one does not.
Console output:
execution(void de.scrum_master.app.Application.sayHelloTo(String))
get(PrintStream java.lang.System.out)
call(java.lang.StringBuilder(String))
call(StringBuilder java.lang.StringBuilder.append(String))
call(StringBuilder java.lang.StringBuilder.append(String))
call(String java.lang.StringBuilder.toString())
call(void java.io.PrintStream.println(String))
Hello world!
Explanation: As you can see there are a number of joinpoints within the control flow of the active advice. Each one of these would again trigger the active advice because each of them again matches the pointcut cflow(execution(* Application.sayHelloTo(..))). The advice is a method like any other, though, it just happens to be inside of an aspect. Anyway, it is in the control flow of its own pointcut which again triggers the advice which again is in its own pointcut's control flow and so forth. Infinite recursion!

RPN with operators other than *,/,+,-

I have a class called Node.
public Node
{
public int data;
public Node primaryNext;
public Node secondaryNext;
}
I have a Node root = null; And when the first value is received from the input, it then runs something like this.
root = new Node;
root.data = /*input*/ ;
root.primaryNext = null;
root.secondaryNext = null;
The next step is adding a new Node at the end of the list, by pointing root.primaryNext or root.secondaryNext to a new Node while filling the "pointer" tree by levels. So I need to do something like this:
GIF of the idea.
I think that this could be done using ||, &&, |, & operators applied to each level of nodes with a recursive method. So:
How do I operate in C# like the RPN?
If I can, which would be the best way to do it? I understand recursion pretty well, but I might not do the best possible method.
Thanks.
Suggestion:
One command to push a single node onto a stack.
Another command to take the two topmost nodes from the stack, combine them and push the result back on the stack.

Flash Player: Get reference count for variable

I'm looking to build a library that needs to be very careful about memory management. Basically, I have 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 FooFactory. FooFactory defines a single method, getFoo(key:String):Foo.
getFoo looks in a private static flash.utils.Dictionary object for the appropriate Foo instance, and either lazy-instantiates it, or simply returns it. In any case, FooFactory MUST keep a reference to each Foo instance created, so all Foo instances can be updated by FooFactory using a method called updateFoos():void.
Here is some pseudo-code of what I'm talking about:
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 && 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 && foos[key] != undefined) {
Foo(foos[key]).dispatchEvent(new Event("update"));
}
}
}
}
The actual function and identity of Foo isn't too important.
What IS 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 did 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 Foo instance which was referenced by the FooFactory, like so: (again, pseudocode)
<?xml version="1.0"?>
<s:Group>
<fx:Script>
<![CDATA[
private static const foo:Foo = FooFactory.getFoo('myfoo');
]]>
</fx:Script>
</s:Group>
What I want to know are the two following things:
Is the pseudo-code above "garbage-collector safe?" IE: Will my modules unload properly and will instances of the Group subclass above get garbage collected?
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?
I'm aware of the flash.sampler API, but I am not sure as to how to use it to count references.
I don't think that the pattern you presented should give you problems GC-wise.
private static const foo:Foo = FooFactory.getFoo('myfoo');
Here, your module has a reference to a Foo instance. That means that this Foo instance won't be collectable as long as your module is not collectable. The module has a reference to foo, so here foo is reachable (if the module is reachable). That's not true the other way round. Even if foo lives forever, it doesn't have a reference to the module, so it won't pint it down.
Of course there could be other stuff going on to prevent your module from being collectable, but foo is not the culprit here, unless foo gets a reference to the module somehow. For instance, the module adds a listener to foo, which for this matter, is the same as writing:
foo.addReference(this); // where this is your module
The fact that you declare the instance as const shouldn't change things per se, either. It only means that the reference stored cannot be changed at a later point. However, if you want to null out foo at some later point, you can't because that would be reassigning the reference; and you can't reassigning a const reference (you should get a compiler error). Now, this does tie foo to module. As long as your module is alive it will have a reference to foo, so foo won't be collectable.
Regarding this line:
private static const foos:Dictionary = new Dictionary(true); //use weak keys for gc
It looks like you're trying to build some kind of cache. I'm not sure you want to use weak refs here. (I could be wrong here because I'm making an assumption, and they say assumption is the mother of all... mistakes, but I digress)
In any case, the effect of this is that if a module gets a Foo and at some point the module is successfully unloaded (I mean, cleaned up from memory), that instance of foo could be collected, provided that no one else has a ref to it (that is, the only way to reach it is through the dictionary key, but since the keys are weak referenced, this ref will not count for the purposes of the GC).
Regarding your second question, I'd recommend the FlexBuilder/FlashBuilder profiler, if FB is available to you. It's not the most intuitive tool, granted, but with some practice it could be really useful to track memory problems. Basically, it will let you know how many instances of a given class were created, how many of those are still alive, what objects have references to these instances and where were all these objects allocated (an option not checked by default when you launch the profiler, buy very handy to track a leak).
PS
Regarding your comment:
Perhaps the real issue is the static
const reference bound by the Group
instance? If that's an issue, I could
simply abstract Foo to an interface,
then create something called
FooWeakReference which would use a
weak dictionary to reference the
actual Foo object. Thoughts?
Adding this extra layer of indirection only complicates things and makes your code less obvious for no gain here, I think. It's easier to consider the life-cycle of your module and define clear points of initialization and finalization. When it's finalized, make sure you remove any reference to the module added to the foo instance (i.e. if you have added listeners on foo, remove them, etc), so your module is collectable independently of the life-cycle of foo.
As a general rule, whenever a weak reference seems to solve a bug in your app, it's masking another one or covering up for a poor design; there are exceptions (and compromises that have to be made sometimes), but weak refs are abused gratuitously if you ask me; not everyone will agree, I know.
Also, weak-refs open a whole new kind of bugs: what happens if that instance you created lazily vanishes before you can use it or worse, while you are using it? Event listeners that stop working under not deterministically reproducible circumstances (e.g. you added a listener to an object that is gone), possible null references (e.g. you are trying to add a listener to an object that no longer exists), etc, etc. Don't drink the weak reference kool-aid ;).
Addedum
In conclusion, as one last question,
is it true for me to say that no AS3
solution exists for counting
references? I'm building a complete
unit-testing suite for this library
I'm building, and if I could do
something like Assert.assertEquals(0,
getReferenceCount(foo)), that would be
rad.
Well, yes. You can't get the reference count of a given object from Actionscript. Even if it were possible, I'm not sure that would help, because reference counting is only a part of how GC works. The other one is a mark and sweep algorithm. So, if an object has a zero ref-count is collectable, but it could have, say, 3 references and still be collectable. To really determine whether an object is collectable or not, you should really be able to hook into the GC routine, I guess, and that's not possible from AS.
Also, this code will never work.
Assert.assertEquals(0, getReferenceCount(foo)
Why? Here you are trying to query some API to know whether an object is collectable or not. Since you can't know that, let's assume this tells you whether an object has been collected or not. The problem is, foo at that point is either null or not null. If it's null, it's not a valid reference, so you can't get any useful information out of it, for obvious reasons. If it's not null, it's a valid reference to an object, then you can access it and it's alive; so you already know the answer to the question you're asking.
Now, I think I undestand your goal. You want to be able to tell, programatically, if you certain objects are being leaked. Up to some extent that's possible. It involves using the flash.sampler API, as you mentioned in your original question.
I suggest you check out the Flash Preload Profiler by jpauclair:
I haven't used it, but it looks like it could be just as good as the FB profiler for memory watching.
Since this is Actionscript code (and since it's open source), you could to use it for what you want. I just skimmed through the code, but I've been able to get a very simple-minded proof of concept by monkey-patching the SampleAnalyzer class:
There's a lot of other things going on in this tool, but I just modified the memory analizer to be able to return a list of the alive objects.
So, I wrote a simple class that would run this profiler. The idea is that when you create an object, you can ask this class to watch it. This objects' allocation id will be looked up in the allocated objects table maintained by the memory profiler and a handle to it will be stored locally (only the id). This id handle will also be returned for convenience. So you can store this id handle and at a later point, use it to check whether the object has been collected or not. Also, there's a method that returns a list of all the handles you added and another one that returns a list of the added handles that point to live objects. A handle will allow you to access the original object (if it hasn't been collected yet), its class and also the allocation stack trace. (I'm not storing the object itself or the NewObjectSample object to avoid accidentally pinning it down)
Now, this is important: this queries for alive objects. The fact that an object is alive doesn't mean it's not collectable. So, this alone doens't mean there's a leak. It could be alive at this point but still it doesn't mean there's a leak. So, you should combine this with forcing GC to get more relevant results. Also, this could be of use if you are watching objects that are owned by you and not shared with other code (or other modules).
So, here's the code to the ProfileRunner, with some comments.
import flash.sampler.Sample;
import flash.sampler.NewObjectSample;
import flash.utils.Dictionary;
class ProfilerRunner {
private var _watched:Array;
public function ProfilerRunner() {
_watched = [];
}
public function init():void {
// setup the analyzer. I just copied this almost verbatim
// from SamplerProfiler...
// https://code.google.com/p/flashpreloadprofiler/source/browse/trunk/src/SamplerProfiler.as
SampleAnalyzer.GetInstance().ResetStats();
SampleAnalyzer.GetInstance().ObjectStatsEnabled = true;
SampleAnalyzer.GetInstance().InternalEventStatsEnabled = false;
SampleAnalyzer.GetInstance().StartSampling();
}
public function destroy():void {
_watched = null;
}
private function updateSampling(hook:Function = null):void {
SampleAnalyzer.GetInstance().PauseSampling();
SampleAnalyzer.GetInstance().ProcessSampling();
if(hook is Function) {
var samples:Dictionary = SampleAnalyzer.GetInstance().GetRawSamplesDict();
hook(samples);
}
SampleAnalyzer.GetInstance().ClearSamples();
SampleAnalyzer.GetInstance().ResumeSampling();
}
public function addWatch(object:Object):WatchHandle {
var handle:WatchHandle;
updateSampling(function(samples:Dictionary):void {
for each(var sample:Sample in samples) {
var newSample:NewObjectSample;
if((newSample = sample as NewObjectSample) != null) {
if(newSample.object == object) {
handle = new WatchHandle(newSample);
_watched.push(handle);
}
}
}
});
return handle;
}
public function isActive(handle:WatchHandle):Boolean {
var ret:Boolean;
updateSampling(function(samples:Dictionary):void{
for each(var sample:Sample in samples) {
var newSample:NewObjectSample;
if((newSample = sample as NewObjectSample) != null) {
if(newSample.id == handle.id) {
ret = true;
break;
}
}
}
});
return ret;
}
public function getActiveWatchedObjects():Array {
var list:Array = [];
updateSampling(function(samples:Dictionary):void {
for each(var handle:WatchHandle in _watched) {
if(samples[handle.id]) {
list.push(handle);
}
}
});
return list;
}
public function getWatchedObjects():Array {
var list:Array = [];
for each(var handle:WatchHandle in _watched) {
list.push(handle);
}
return list;
}
}
class WatchHandle {
private var _id:int;
private var _objectProxy:Dictionary;
private var _type:Class;
private var _stack:Array;
public function get id():int {
return _id;
}
public function get object():Object {
for(var k:Object in _objectProxy) {
return k;
}
return null;
}
public function get stack():Array {
return _stack;
}
public function getFormattedStack():String {
return "\t" + _stack.join("\n\t");
}
public function WatchHandle(sample:NewObjectSample) {
_id = sample.id;
_objectProxy = new Dictionary(true);
_objectProxy[sample.object] = true;
_type = sample.type;
_stack = sample.stack;
}
public function toString():String {
return "[WatchHandle id: " + _id + ", type: " + _type + ", object: " + object + "]";
}
}
And here's a simple demo of how you'd use it.
It initializes the runner, allocates 2 Foo objects and then, after 2 seconds, it finalizes itself. Note that in the finalizer, I'm nulling out one of the Foo objects and finalizing the profiler. There I try to force GC, wait for some time (GC is not synchronous) and then check if these objects are alive. The first object should return false, and the second true. So, this is the place were you'd put your assert. Keep in mind that all of this will only work in a debug player.
So, without any further addo, here's the sample code:
package {
import flash.display.Sprite;
import flash.sampler.NewObjectSample;
import flash.sampler.Sample;
import flash.system.System;
import flash.utils.Dictionary;
import flash.utils.setTimeout;
public class test extends Sprite
{
private var x1:Foo;
private var x2:Foo;
private var _profiler:ProfilerRunner;
private var _watch_x1:WatchHandle;
private var _watch_x2:WatchHandle;
public function test()
{
init();
createObjects();
setTimeout(finalize,2000);
}
public function init():void {
initProfiler();
}
public function finalize():void {
x1 = null;
finalizeProfiler();
}
private function initProfiler():void {
_profiler = new ProfilerRunner();
_profiler.init();
}
private function finalizeProfiler():void {
// sometimes, calling System.gc() in one frame doesn't work
// you have to call it repeatedly. This is a kind of lame workaround
// this should probably be hidden in the profiler runner
var count:int = 0;
var id:int = setInterval(function():void {
System.gc();
count++;
if(count >= 3) {
clearInterval(id);
destroyProfiler();
}
},100);
}
private function destroyProfiler():void {
// boolean check through saved handles
trace(_profiler.isActive(_watch_x1));
trace(_profiler.isActive(_watch_x2));
// print all objects being watched
trace(_profiler.getWatchedObjects());
// get a list of the active objects and print them, plus the alloc stack trace
var activeObjs:Array = _profiler.getActiveWatchedObjects();
for each(var handle:WatchHandle in activeObjs) {
trace(handle);
trace(handle.getFormattedStack());
}
_profiler.destroy();
}
private function createObjects():void {
x1 = new Foo();
x2 = new Foo();
// add them for watch. Also, let's keep a "handle" to
// them so we can query the profiler to know if the object
// is alive or not at any given time
_watch_x1 = _profiler.addWatch(x1);
_watch_x2 = _profiler.addWatch(x2);
}
}
}
import flash.display.Sprite;
class Foo {
public var someProp:Sprite;
}
Alternatively, a more light-weight approach for tracking alive objects is storing them in a weak-referenced dictionary, forcing GC and then checking how many objects are stil alive. Check out this answer to see how this could be implemented. The main difference is that this gives you less control, but maybe it's good enough for your purposes. Anyway, I felt like giving the other idea a shot, so I wrote this object watcher and kind of like the idea.
Since you essentially want weak references, perhaps the best solution would involve one of the weak references available in AS3.
For example, have your method store Dictionaries rather than the actual objects. Something like this:
private var allFoos:Dictionary;
public function getFoo(key:String):Foo {
var f:Foo = _getFoo(key);
if (f == null) {
f = _createFoo(key);
}
return f;
}
private function _createFoo(key:String):Foo {
var f:Foo = new Foo();
var d:Dictionary = new Dictionary(/* use weak keys */ true);
d[f] = key;
allFoos[key] = d;
}
With some intense thinking over the weekend, I believe I figured out what the problem is.
Essentially, we have this scenario:
.--------------.
| APP-DOMAIN 1 |
| [FooFactory] |
'--------------'
|
| < [object Foo]
|
.--------------.
| APP-DOMAIN 2 |
| [MyModule] |
'--------------'
APP-DOMAIN 1 always stays in memory, since it's loaded in the highest app-domain possible: the original compiled code of a SWF. APP-DOMAIN 2 is loaded into and out of memory dynamically and must be able to completely sever itself from APP-DOMAIN 1. According to the genius answer above by Juan Pablo Califano, APP-DOMAIN 2 having a reference to [object Foo] doesn't necessarily tie APP-DOMAIN 2 into memory, though it could become tied into memory by [MyModule] adding an event listener to [object Foo], right?
Okay, so, with this in mind, an overkill solution would be to return a weak-reference-implementation of Foo from the getFoo method, since that's where things need to "break off" in case of "emergency." (Things need to be weak from this perspective so that APP-DOMAIN 1 can be garbage collected completely as it is unloaded.) Again, this is an overkill answer.
However, I do not need to keep a weak-ref to Foo in FooFactory, since FooFactory needs to have a surefire way of getting a hold of each created Foo object. In short, Juan Pablo Califano has the theory completely right, it just needs to be tested in the real world in order to prove everything definitively :)
All of this aside, I believe I have uncovered the real issue behind the scenes that caused a similar library I wrote in the past to never GC. The problem was not in the actual library I wrote, but it seems that it was in a reflection library I was using. The reflection library would "cache" every Class object I threw at it, since my original FooFactory.getFoo method took a Class parameter, rather than a String. Since the library seemed to be hard-referencing every Class object passed into memory, I'm pretty sure that was the memory leak.
In conclusion, as one last question, is it true for me to say that no AS3 solution exists for counting references? I'm building a complete unit-testing suite for this library I'm building, and if I could do something like Assert.assertEquals(0, getReferenceCount(foo)), that would be rad.

Resources