|
From: Rimshot on 9 Apr 2008 11:25 I am wondering if someone can help me understand why I should or should not be concerned about returning local variables from a called component - at least for datatypes that are passed by reference rather than value. I am currently refraining from doing this, since it appears that the calling routine would receive back a pointer to memory that the CFC method would have released back to the operating system upon its completion. I've attached an example of what I'm talking about. When the code is finished running, I expect mystruct will be a pointer to the foo structure, which may or may not have been released to the operating system after mycfcmethod completes. So far I have been dealing with this safely, I believe, by always returning a duplicated version of the results, such as Duplicate(foo). I would like to avoid this extra overhead if it is unnecessary. Possibly CF knows to hold onto any memory that is referenced elsewhere, or possibly it doesn't. Any insights would be appreciated. Calling Code: <cfset mystruct = mycfcmethod()> ------------------------ Hypothetical CFC Method: <cffunction name="mycfcmethod" output="no" returntype="struct" access="public"> <cfset var foo = StructNew()> <cfset foo["color"] = "Red"> <cfset foo["size"] = "large"> <cfreturn foo> </cffunction>
From: GArlington on 9 Apr 2008 11:45 On Apr 9, 4:25 pm, "Rimshot" <webforumsu...(a)macromedia.com> wrote: > I am wondering if someone can help me understand why I should or should not be > concerned about returning local variables from a called component - at least > for datatypes that are passed by reference rather than value. I am currently > refraining from doing this, since it appears that the calling routine would > receive back a pointer to memory that the CFC method would have released back > to the operating system upon its completion. > > I've attached an example of what I'm talking about. When the code is finished > running, I expect mystruct will be a pointer to the foo structure, which may or > may not have been released to the operating system after mycfcmethod completes. > > So far I have been dealing with this safely, I believe, by always returning a > duplicated version of the results, such as Duplicate(foo). I would like to > avoid this extra overhead if it is unnecessary. Possibly CF knows to hold onto > any memory that is referenced elsewhere, or possibly it doesn't. > > Any insights would be appreciated. > > Calling Code: > <cfset mystruct = mycfcmethod()> > > ------------------------ > Hypothetical CFC Method: > <cffunction name="mycfcmethod" output="no" returntype="struct" access="public"> > <cfset var foo = StructNew()> > <cfset foo["color"] = "Red"> > <cfset foo["size"] = "large"> > <cfreturn foo> > </cffunction> You should read some about how Java gc() works: [hint] it counts ALL references to the variable/memory...
From: Ian Skinner on 9 Apr 2008 11:45 Rimshot wrote: > Any insights would be appreciated. > > Calling Code: > <cfset mystruct = mycfcmethod()> > > ------------------------ > Hypothetical CFC Method: > <cffunction name="mycfcmethod" output="no" returntype="struct" access="public"> > <cfset var foo = StructNew()> > <cfset foo["color"] = "Red"> > <cfset foo["size"] = "large"> > <cfreturn foo> > </cffunction> > You did not show the use of duplicate() in this example, but it is not necessary. Buried somewhere deep on the documentation is the description that ColdFusion will pass simple values by reference and complex values by pointer. So you will end up with 'mystruct' pointing to the same memory defined for 'foo'.
From: Rimshot on 9 Apr 2008 12:27 Thank you for your response Ian. I am a bit confused by your answer, though, in that a structure is a complex variable, and therefore would point back to the released foo memory. As such a separate copy of the structure would need to be returned...no? Your comment indicated that Duplicate would not be needed. Sorry for my lack of clarity regarding my use of Duplicate. I am using it in my return statemet within the method: <cfreturn Duplicate(foo)>
From: Ian Skinner on 9 Apr 2008 12:39
Rimshot wrote: > Thank you for your response Ian. > > I am a bit confused by your answer, though, in that a structure is a complex > variable, and therefore would point back to the released foo memory. As such a > separate copy of the structure would need to be returned...no? Your comment > indicated that Duplicate would not be needed. > > Sorry for my lack of clarity regarding my use of Duplicate. I am using it in > my return statemet within the method: > > > <cfreturn Duplicate(foo)> > Java, which is the foundation of ColdFusion, will not release a piece of memory until ALL pointers to it are removed. Thus once 'mystruct' was attached to the structure, garbage collection is told hands off while it still has a reference. That is my basic understanding at least. |