In the past, I've mentioned how CF will pass a structure by reference if you do something like the following:
a = structNew();
b = a;
However, a coworker found something interesting. Inside a UDF, she did something like so:
var a = structNew();
a.orig = "foo";
application.foo = a;
What was interesting was that even though 'a' was a var scoped variable, the value of application.foo still existed after the request. I did another test and discovered that even outside of a UDF, if you set an application struct to point to a variables scope struct, the "death" of the variables scope struct simply "tears off" a copy to the application scope. I can't see a real use for this, but I figured I'd share it.
Updated Even more interesting, if you make B point to A, ie, a is a struct, b = a, and then change the type of a, than CF "tears" it again. Consider:
s = structnew();
s.name = "orig";
b = s;
s=arraynew(1);
writeoutput(b.name);
This will output orig and not throw an error as you might expect.
Archived Comments
If you consider that each variable holds a reference to the actual struct in memory, then the behavior you see is simple to explain and what you should expect.
If you make application.foo 'point at' var a, what it really points at is the underlying memory which won't disappear until *all* references to it disappear.
Same with the s/b example. You are simply making s point at some new object - b will continue to point at the old one.
+1, Sean.
The first example is explained by garbage collection (or ref counting), the second example by the fact that variables just hold references to the objects. This is exactly consistent with what you see in Java, Ruby, and any number of other languages--though probably not C. :)
Makes perfect sense, Sean, thanks for taking the time to explain it!