Reference stored in Server scope does not get deleted

When I create a reference to a complex structure I was always under the impression that the reference would be destroyed if the original is destroyed, but it seems this is not the case. Can anyone confirm whether this is normal behaviour or not?

What I am trying to do is set a complex var in the application scope, create a reference to it in the server scope and was hoping it would get destroyed when the application times out, but its not working, unfortunately..
LVL 17
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Correct me if I'm wrong, but I believe it works like this: everything you create is a reference to an object. If you do application.complexvar = complexstruct, then application.complexvar is already a reference to complexstruct. Knowing this, you can easily work out the rest: server.complexvar = application.complexvar will give you another reference to complexstruct. And of course the object is destroyed only then, when the last reference is deleted.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tacobell777Author Commented:
not everything is a reference, only complex objects (I believe), whatever the case, you are repeating what I already said.
The problem is that the reference does not get destroyed while the original object in the application is...

Hoi Taco,

You're right in that not everything is a reference. I was refering to complex objects (structs, array, cfc's, queries), just forgot to actually write that...

As for me repeating you: I didn't. I just tried to make clear that <cfset application.somevar = structNew()> already is a reference. The original in this case is structNew(), not application.somevar. There is in this case no way (that I know of) to destroy the original. It's only possible to destroy each and every reference. (Removing the last reference will also remove the original of course)

So when you create a complex object...

<cfapplication name="test" sessionmanagement="Yes">
<cfset application.test = structNew()>
<cfset application.test.blabla = "ingewikkelde structure">

... application.test is merely a reference to it.

<cfset session.test = application.test>

... now session.test is another reference to it.

<cfset structdelete(application, "test")>

... deleting the first reference, but keeping the object, since there's still a reference remaining as is proved by:

<cfdump var="#session#" />
<cfdump var="#application#" />

Snappie? :)
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tacobell777Author Commented:
Exactly, and that is the whole problem I am having...

If you update the original object then the reference is *also* updated, but when you delete the original object you would expect the reference to be deleted as well, wouldn't you??

My questions is:
is this normal behaviour, yes or no?

Snapu ?
Niet verbaast hoe ik weet dat je nederlands bent?
You can't delete the original (complex) object directly.

What you are deleting is the first reference you made to it. And as long as there are other references to it, the object will remain intact.
In <cfset application.somevar = structNew()> the object is not application.somevar, it is structNew(). Therefore, if you delete application.somevar, you're deleting a reference. But I think that is clear now. We just have different ways of describing the issue.

Is is normal behaviour? I think it is. What you're really asking is: should there be a way to delete/access the original directly (rather than the first refence to it)? Maybe. I'm not sure whether it would violate some computer law or not. It would surely solve your problem. However, I'm not sure whether it should be solved that way in the first place.
I know, it's no definitve answer yet, but it's still early...

> Niet verbaast hoe ik weet dat je nederlands bent?
Iets zegt me dat je daar niet op gekomen bent vanwege m'n naam. In dit geval begin ik het me wel af te vragen... :)
Tacobell777Author Commented:
I also posted this question to some forums, no definate answer there either ;-)

Ruud is toch echt een nederlandse naam (volgens mij?) ;-)
Bij mijn weten ook. :) Heb m'n profiel er ook maar ff op aangepast.

Have you tried your question with the people who build ColdFusion? Surely they have an answer...

Tacobell777Author Commented:
And the answer is..........

The object exists as long as a reference to it exists, so its normal behaviour, and I can't do what I wanted to do :-((
Hi ee_ai_construct,
The answer from TacoBell is good one. I never knew this and learnt from this thread.
I will suggest PAQ/Refund.

Hmm, what about my efforts to explain WHY TacoBell's problem occurs? Surely, it does not solve the situation, but then again, you don't have a garantuee that someone does provide a solution.

Besides, he never told what he wanted to achieve, using this technique. Therefore leaving us no chance to provide an alternative.

And I was still looking into this question, so you might not want to close this question to soon.
Tacobell777Author Commented:
Mosphat, no offense, I did explain what I wanted:

"What I am trying to do is set a complex var in the application scope, create a reference to it in the server scope and was hoping it would get destroyed when the application times out, but its not working, unfortunately.."

What you explained to me I already knew, the only question was why a reference is updated but not deleted when one or the object is destroyed...

But if you really wants the points you can have them..
Taco, no offense taken :)

You explained you wanted the reference in the server scope to be destroyed when the original object is destroyed. But what is it that you need to have these references in the server scope for in the first place? Maybe if you describe the larger picture, we can find a possible alternative solution there.

As for explaining what you already know: you've found out that the reference is not deleted, when you delete the object. But what I tried to explain is: you can't really delete the object, since you only have access to it through references. If you do structDelete(application, "complexStruct") or even if the application scope times out, you're not deleting the object, it's just the reference that's being destroyed. The object itself still resides in memory somewhere, because the server-reference keeps pointing to it. Once that's gone too, the object is destroyed.

The points are only of interest to me, if my answer(s) helped you gain more insight. If not, I'd like to know why not (and probably try again :) )
Tacobell777Author Commented:
ok, so you want to know the "why" behind all this.
The why is:
I have some settings in the server scope that I want destroyed when the application times out, as for another solution for that, I already knew of one before I aksed this question, I just wanted to see why my first idea was not possible. (the other solution is writing a javasclass as a listener)

When I read some of my posts I see that my wording is a bit wrong, which might have confused you.

Lets assign some points as what you are saying is part of the answer..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.