Wednesday 5 December 2012

Forgot to say

G'day:
OK, I often say "this'll be a quick one", and then waffle on for a coupla thousand words. Not this time: it'll be quick.

One thing I forgot to mention in the "I love being wrong" article I posted a few hours ago was how I determined that the form scope was indeed being deleted and reconstructed every request, not simply being cleared (because as I mentioned: it's being both cleared and deleted, which seems logically tautological, if you see what I mean).


I wanted to check if the form scope was the same object every request, so I tried to get its hashcode, which should be a unique identifier for the object. Running this:

<cfoutput>
#form.hashCode()#
</cfoutput>

Yields just "0". Not helpful.

So I use ClassViewer to have a shufti around inside the implementation of the form scope (coldfusion.filter.FormScope), and didn't see anything useful, so I googled about and found that one can call java.lang.System's identityHashCode() method, which ignores however hashCode() is implemented in the object's class, and just returns the hashcode (as implemented by Object.class, I guess). Cool.

I knocked together this code:

<cfset system = createObject("java", "java.lang.System")>
<cfoutput>
    server: #system.identityHashCode(server)#<br />
    application: #system.identityHashCode(application)#<br />
    session: #system.identityHashCode(session)#<br />
    URL: #system.identityHashCode(URL)#<br />
    form: #system.identityHashCode(form)#<br />
    request: #system.identityHashCode(request)#<br />
    variables: #system.identityHashCode(variables)#<br />
</cfoutput>

And this outputs this sort of thing:

server: 1793701383
application: 1275673498
session: 715270222
URL: 305777599
form: 275739122
request: 77713228
variables: 456165620

If one refreshes the page, one sees that the server, application and session object IDs all stay the same across requests, all the other ones change. So this indicates the persistent scope objects are the same across requests (obviously), but the other ones are definitely recreated, not simply cleared and reused.  Also note I let the session and applications time out, and saw those IDs change when I loaded the page.

So, bottom line, I don't know why ColdFusion needs to clear the form scope after a request, it could just delete its reference and let it be GCed if the underlying data is no longer being used by something else. This would allow other references to it to still work, and in the next request a new form scope is created.

This is a very minor thing, but I "enjoyed" researching it. I agree it's not hugely interesting though ;-)

See? Pretty short.

--
Adam