<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Table expression - Valid v/s Invalid data references in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472055#M15244</link>
    <description>&lt;P&gt;Slightly off topic but i couldn't find any difference between generic types - &lt;EM&gt;any&lt;/EM&gt; &amp;amp; &lt;EM&gt;data&lt;/EM&gt;? Is there a guideline on when to use which one?&lt;/P&gt;</description>
    <pubDate>Fri, 14 Jul 2017 08:03:42 GMT</pubDate>
    <dc:creator>SuhaSaha</dc:creator>
    <dc:date>2017-07-14T08:03:42Z</dc:date>
    <item>
      <title>Table expression - Valid v/s Invalid data references</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472050#M15239</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
  &lt;P&gt;I ran into a ABAP trapdoor today. Here goes the code snippet:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;CLASS lcl_test_memory DEFINITION CREATE PUBLIC.


  PUBLIC SECTION.
    DATA: clients TYPE SORTED TABLE OF t000
                  WITH UNIQUE KEY mandt
                  READ-ONLY.
    METHODS:
      constructor,
      get_invalid_ref
        IMPORTING
          iv_client      TYPE symandt
        RETURNING
          VALUE(rd_data) TYPE REF TO data,
      get_valid_ref
        IMPORTING
          iv_client      TYPE symandt
        RETURNING
          VALUE(rd_data) TYPE REF TO data.
  PROTECTED SECTION.
  PRIVATE SECTION.


ENDCLASS.


CLASS lcl_test_memory IMPLEMENTATION.


  METHOD constructor.
    SELECT *
      FROM t000
      INTO TABLE @clients.
  ENDMETHOD.


  METHOD get_invalid_ref.


    DATA(ls_client) = VALUE #( clients[ mandt = iv_client ] ).


    rd_data = REF #( ls_client ).


  ENDMETHOD.


  METHOD get_valid_ref.
    rd_data = REF #( clients[ mandt = iv_client ] ).
  ENDMETHOD.


ENDCLASS.


START-OF-SELECTION.
  DATA(gd_invalid) = NEW lcl_test_memory( )-&amp;gt;get_invalid_ref( sy-mandt ).
  DATA(gd_valid) = NEW lcl_test_memory( )-&amp;gt;get_valid_ref( sy-mandt ).
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I have already read the blogs from &lt;A target="_blank" href="https://blogs.sap.com/2016/10/14/invalid-references/"&gt;Horst &lt;/A&gt;and &lt;A target="_blank" href="https://blogs.sap.com/2017/04/01/is-bound-is-initial-and-is-assigned/"&gt;Jerry&lt;/A&gt;, so the cause of the "invalid" data reference is already clear. Although i must admit that i had already been through the trapdoor &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
  &lt;P&gt;However when i use the REF operator together with the table expression inline, i get back a valid data reference. Why?&lt;/P&gt;
  &lt;P&gt;As usual i looked up the &lt;A target="_blank" href="https://help.sap.com/http.svc/rc/abapdocu_750_index_htm/7.50/en-US/abapget_reference.htm"&gt;ABAP documentation&lt;/A&gt;, what stuck me is the following - &lt;/P&gt;
  &lt;P&gt;&lt;EM&gt;When applied to data objects in the heap GET REFERENCE creates memory-retaining heap references.&lt;/EM&gt;&lt;/P&gt;
  &lt;P&gt;Since "heap" contains anonymous data objects, does the table expression generate an anonymous data object? I looked up the documentation and couldn't find anything which answers my question.&lt;/P&gt;
  &lt;P&gt;Can anyone provide me an explanation? How does one avoid this trapdoor?&lt;/P&gt;
  &lt;P&gt;BR,&lt;/P&gt;
  &lt;P&gt;Suhas&lt;/P&gt;
  &lt;P&gt;PS - I am working on ABAP 750 SP06 release&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 14:49:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472050#M15239</guid>
      <dc:creator>SuhaSaha</dc:creator>
      <dc:date>2017-07-13T14:49:41Z</dc:date>
    </item>
    <item>
      <title>Re: Table expression - Valid v/s Invalid data references</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472051#M15240</link>
      <description>&lt;P&gt;Not much of a trapdoor I don't think. In your second method you "retain" a valid data reference because you did not copy your table expression result to a local variable that is garbage collected / out of scope once you leave the method. In get_invalid_ref the reference points towards the local variable! The instance attribute clients still exists after you leave the method.&lt;/P&gt;
  &lt;P&gt;The memory retaining heap references I think just means that the object instance with the referenced attribute will not be garbage collected once you free all the references to it because data references to member attributes of it also count towards the "reference counter" which needs to be zero for garbage collection to be able to occur.&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/50418-debug.png" /&gt;&lt;/P&gt;
  &lt;P&gt;As you can see in the screenshot the second instance of lcl_test_memory is still in memory (0:8*) even though go_b is cleared and manual garbage collection was invoked.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 18:20:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472051#M15240</guid>
      <dc:creator>fabianlupa</dc:creator>
      <dc:date>2017-07-13T18:20:56Z</dc:date>
    </item>
    <item>
      <title>Re: Table expression - Valid v/s Invalid data references</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472052#M15241</link>
      <description>&lt;P&gt;&lt;A href="https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenheap_glosry.htm"&gt;Heap&lt;/A&gt;, "Part of the program memory in which anonymous data objects and &lt;STRONG&gt;instances of classes&lt;/STRONG&gt; created dynamically using CREATE DATA, &lt;STRONG&gt;CREATE OBJECT, or NEW&lt;/STRONG&gt; are saved. The allocated memory can be released again by the garbage collector."&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; The instance of your class lies in the heap. What is the instance of a class else but it's attributes?&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; As you say yourself:&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; "&lt;EM&gt;When applied to data objects in the heap GET REFERENCE creates memory-retaining heap references.&lt;/EM&gt;" &lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; You apply REF (same as GET REFERENCE) to the instance attribute of an object in the heap and by that keep the whole object alive.&lt;/P&gt;
  &lt;P&gt;&lt;A href="https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenheap_reference_glosry.htm"&gt;heap reference&lt;/A&gt;: Reference to an object &lt;STRONG&gt;or part of an object&lt;/STRONG&gt; in the heap. A heap reference keeps an object alive.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2017 06:22:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472052#M15241</guid>
      <dc:creator>retired_member</dc:creator>
      <dc:date>2017-07-14T06:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: Table expression - Valid v/s Invalid data references</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472053#M15242</link>
      <description>&lt;P&gt;Yep &lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2017 06:23:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472053#M15242</guid>
      <dc:creator>retired_member</dc:creator>
      <dc:date>2017-07-14T06:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: Table expression - Valid v/s Invalid data references</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472054#M15243</link>
      <description>&lt;P&gt;Thanks for the clarification!&lt;/P&gt;
  &lt;P&gt;I always thought that the "functional" operators create anonymous data objects, hence "trapdoor".&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2017 07:41:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472054#M15243</guid>
      <dc:creator>SuhaSaha</dc:creator>
      <dc:date>2017-07-14T07:41:27Z</dc:date>
    </item>
    <item>
      <title>Re: Table expression - Valid v/s Invalid data references</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472055#M15244</link>
      <description>&lt;P&gt;Slightly off topic but i couldn't find any difference between generic types - &lt;EM&gt;any&lt;/EM&gt; &amp;amp; &lt;EM&gt;data&lt;/EM&gt;? Is there a guideline on when to use which one?&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2017 08:03:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472055#M15244</guid>
      <dc:creator>SuhaSaha</dc:creator>
      <dc:date>2017-07-14T08:03:42Z</dc:date>
    </item>
    <item>
      <title>Re: Table expression - Valid v/s Invalid data references</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472056#M15245</link>
      <description>&lt;P&gt;Used directly behind &lt;STRONG&gt;TYPE&lt;/STRONG&gt;, there is no difference:&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;TYPE any&lt;/STRONG&gt; is the same as &lt;STRONG&gt;TYPE data&lt;/STRONG&gt;.(for historic reasons).&lt;/P&gt;
  &lt;P&gt; Behind &lt;STRONG&gt;REF TO&lt;/STRONG&gt;, there is a difference:&lt;/P&gt;
  &lt;P&gt; You can say &lt;STRONG&gt;TYPE REF TO data&lt;/STRONG&gt; and &lt;STRONG&gt;TYPE REF TO object&lt;/STRONG&gt; but not &lt;STRONG&gt;TYPE REF TO any&lt;/STRONG&gt;.&lt;/P&gt;
  &lt;P&gt; &lt;STRONG&gt;TYPE REF TO any&lt;/STRONG&gt; would be a generic reference variable that could point to data objects and instances of classes. This was never implemented, because then the question arises, what is the meaning of &lt;STRONG&gt;TYPE any&lt;/STRONG&gt;, which - unfortunately - is older than &lt;STRONG&gt;TYPE data&lt;/STRONG&gt;.&lt;/P&gt;
  &lt;P&gt;From that said, it would clearer to use &lt;STRONG&gt;TYPE data&lt;/STRONG&gt;, but in fact nobody cares ...&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2017 08:18:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472056#M15245</guid>
      <dc:creator>retired_member</dc:creator>
      <dc:date>2017-07-14T08:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Table expression - Valid v/s Invalid data references</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472057#M15246</link>
      <description>&lt;P&gt;Thanks for clarifying it up! &lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2017 09:45:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/table-expression-valid-v-s-invalid-data-references/m-p/472057#M15246</guid>
      <dc:creator>SuhaSaha</dc:creator>
      <dc:date>2017-07-14T09:45:01Z</dc:date>
    </item>
  </channel>
</rss>

