M Lamb
M Lamb

Reputation: 199

Just how global are Coldfusion variables not declaring using “var”?

I’m using Coldfusion MX 8. I recently had a situation where variables seem to be “swapping” between sessions. I found some information regarding entire sessions swapping, but this was not the case. It was just one variable being swapped, not the entire session. My code snippets follow:

var idArray = ListToArray(arguments.event.getArg("itemIDs"));
var oItemDetail = 0;
var oItem = 0; //Inserting this line seems to have fixed the error.
var i = 0;

for (i=1;i lte ArrayLen(idArray);i=i+1) {

    //Log Spot #1 – cflog idArray[i] and arguments.event.getArg("statusNotes")

    oItem = getItemService().getItem(idArray[i]);
    oItemDetail = getItemService().getItemDetail();
    oItemDetail.setItemID(oItem.getItemID());

    oItemDetail.setStatusNotes(arguments.event.getArg("statusNotes"));

    getItemService().saveItem(oItem);
    getItemService().saveItemDetail(oItemDetail);
}

//getItem and getItemDetail just call getTransfer().get()
//saveItem and saveItemDetail just call getTransfer().save()

For example, at Log Spot #1, idArray[i] might have been “1”, and the StatusNotes event arg might be “abc”.

But should another person, in another session, using another login, in another place, another browser, etc.etc. Use the function at exactly the same time, using idArray[i] = “2” and statusNotes = “def”, then Item Detail “abc” might instead attach to Item “2”, and Item Detail “def” attach to Item “1”.

The fact that, at Log Spot #1, the variables logged are correct, but in the database they are swapped, points to these lines of code as the suspects.

This problem has gone away by declaring “var oItem” at the top.

So I guess I’m a little shocked by this revelation. I would assume that not declaring my local variables would mean another variable, with the same name, in another function, but in the same session might get overwritten. But this seems to be some sort of internal memory issue. The variables are not even being overwritten, rather swapped, between sessions!

I was wondering if anyone had any similar experiences and could shed some light on this?

Upvotes: 1

Views: 292

Answers (1)

Jason Dean
Jason Dean

Reputation: 9615

Unvar'd variables are made private variables within the object that they are contained in. Which causes two problems,

  1. They are shared (accessed and written to) by functions (within that same component)
  2. They live beyond the life of the function call

When you var a variable it makes it a local variable only to that function. Only that function can use it and it only lives as long as that function does.

In your case, this problem doesn't really have anything to do with sessions, other than that is the persistent scope where you happen to be storing the data from these functions.

You said

I would assume that not declaring my local variables would mean another variable, with the same name, in another function, but in the same session might get overwritten.

But it would be more accurate to say

I would assume that not declaring my local variables would mean another variable, with the same name, in another function, but in the same OBJECT might get overwritten.

Upvotes: 4

Related Questions