Skip to main content
Participant
May 29, 2012
Answered

CF 9MX Object Destruction

  • May 29, 2012
  • 1 reply
  • 722 views

Hello all.

I am a brand new CF developer (9MX), and have have not been able to Google for an answer to my question:

How does one destroy an object such as a component object?

I can use <cfset foo = new bar()  /> for creation of a new component object, but can find zero references as to how an object is destroyed (if it's even possible).

I want to destroy an object when finished with it, then create a new one (which will have a different valid base state).  While I don't use a large number of them, I'm trying to avoid the overhead associated with having objects which are no longer in use while others are being created.

Thanks.

    This topic has been closed for replies.
    Correct answer Steve Sommers

    Coldfusion automatically destroys objects when they are no longer referenced. In your example, <cfset foo = 0 /> will do the trick if you want to do it yourself otherwise it'll self destruct when the request terminates, assuming that "foo" is in the variables scope (although, it's best practice to explicitly scope your variables - <cfset variables.foo = 0 />.

    Also, session or application scoped objects will destructs when the session or application timeout occurs or you clear them via a reassigment or using structDelete().

    1 reply

    Steve SommersCorrect answer
    Legend
    May 29, 2012

    Coldfusion automatically destroys objects when they are no longer referenced. In your example, <cfset foo = 0 /> will do the trick if you want to do it yourself otherwise it'll self destruct when the request terminates, assuming that "foo" is in the variables scope (although, it's best practice to explicitly scope your variables - <cfset variables.foo = 0 />.

    Also, session or application scoped objects will destructs when the session or application timeout occurs or you clear them via a reassigment or using structDelete().

    GKWildAuthor
    Participant
    May 29, 2012

    Thank you for the response.

    I was under the impression that CF would do it's housekeeping (object destruction) of the objects once there were no longer any references, so I was good there it appears.

    I did not know how to explicitly destroy an object though. 

    Also - my example was Q&D.  I do in fact use explicit scope.

    Trying to learn good habits with my new language, so feedback of that type is appreciated (as well as the answer to my question).