• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Returning local method variables from a CFC

Explorer ,
Apr 09, 2008 Apr 09, 2008

Copy link to clipboard

Copied

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.
TOPICS
Advanced techniques

Views

381

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 09, 2008 Apr 09, 2008

Copy link to clipboard

Copied

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'.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 09, 2008 Apr 09, 2008

Copy link to clipboard

Copied

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:

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 09, 2008 Apr 09, 2008

Copy link to clipboard

Copied

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.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 09, 2008 Apr 09, 2008

Copy link to clipboard

Copied

LATEST
Memory in your calling templates is reserved for a structure when you call mycfcmethod. That is why you specify the signature Structure mycfcmethod(). So, that returned structure will remain available during the scope of the calling template's use of the structure.

Honestly, I am not sure if this means that CF duplicates the structure or if a pointer keeps the initial memory from being collected. It would seem to be the latter. Regardless, your sample code should work. You should not find that structure disappears unexpectedly.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation