DevWebProSE
DevWebProSE Newsletter:
Last Updated:


Get Your Site Submitted for Free in the World's Largest B2B Directory!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions


A Var Scope Screw Up

By Raymond Camden
Expert Author
Article Date: 2007-07-09

I'm a big fan of sharing mistakes as I figure any mistake I make I'm sure others have as well.

If you read my blog or have attended any presentation I do on ColdFusion Components, you know I get pretty anal about var scoping. One of the things I mention is how if you don't var scope - some day - some where - a bug is going to bite you in the rear and it may takes days to track it down. Well guess what? It happened to me.

For about two months now I've gotten daily bug emails from RIAForge. The bug always looked a bit like this:
The NAME parameter to the setNAME function is required but was not passed in.
I was never able to reproduce the bug, and as far as I knew, it wasn't a bug that could exist. Here is the code in question:

<cfquery name="getit" datasource="#dsn#">
  select stuff,changed,here
  from users
  where id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.id#">
</cfquery>

<cfif getit.recordCount>
  <cfloop index="col" list="#getit.columnlist#">
      <cfinvoke component="#bean#" method="set#col#">
          <cfinvokeargument name="#col#" value="#getit[col][1]#">
      </cfinvoke>
  </cfloop>
</cfif>


This code is in my DAO CFC in the read method. I use the fact that my database columns has the same names as my bean. This lets me easily set all the values in the bean by just looping over the column list from the query.

Looking at this code, it seems impossible for me to run setX and not pass X. As you can plainly see, the method name and argument share the exact same value.

The only thing I figured was that maybe the value in the query was null, and ColdFusion was saying, "Yea, you passed me X, but it was null, so you didn't really pass X."

Turns out - I had forgotten a simple var scope on "col". Under load, it was possible for the col values not to match up and then throw my error.

So - just consider this a warning!

Comments

About the Author:
Raymond Camden, ray@camdenfamily.com
http://ray.camdenfamily.com

Raymond Camden is Vice President of Technology for roundpeg, Inc. A long time ColdFusion user, Raymond has worked on numerous ColdFusion books and is the creator of many of the most popular ColdFusion community web sites. He is an Adobe Community Expert, user group manager, and the proud father of three little bundles of joy.



Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact | Site Map

DevWebProSE is an iEntry, Inc.® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal