‎2014 Jun 05 3:42 AM
Hi Experts,
Currently I have got a issue, say a class method defined a exporting parameter with type "TYPE REF TO DATA", and in the implementation of method, I declare a local variable with a specific data type( e.g. data lv_str type string ), and then use statement "GET REFERENCE" to return this local variable's reference to the exporting parameter;
but when I call the method, this exporting parameter get nothing, and after some debugging, i found the parameter's content is "FREED STACK:{......}";
but if I change the code within the method implementation like this, declare a local reference, and use statement "CREATE DATA"( e.g. DATA lr_str type ref to string. CREATE DATA lr_str. ), and then give the local reference value to the exporting parameter, it works fine.
I also write a Z-program to show this issue:
REPORT ztest_get_reference.
CLASS lcl_test_ref DEFINITION .
PUBLIC SECTION .
TYPES :
BEGIN OF ty_bas_data ,
f1 TYPE string ,
f2 TYPE string ,
END OF ty_bas_data .
TYPES :
BEGIN OF ty_ref ,
index TYPE i ,
ref_data TYPE REF TO data ,
END OF ty_ref .
CLASS-METHODS test_get_ref_to
RETURNING VALUE ( rs_ref) TYPE ty_ref.
CLASS-METHODS test_create_ref
RETURNING VALUE ( rs_ref) TYPE ty_ref.
ENDCLASS .
CLASS lcl_test_ref IMPLEMENTATION .
METHOD test_get_ref_to.
DATA : ls_data TYPE ty_bas_data .
rs_ref- ref_data = REF # ( ls_data ).
ENDMETHOD .
METHOD test_create_ref.
DATA : lr_data TYPE REF TO ty_bas_data .
CREATE DATA lr_data .
rs_ref- ref_data = lr_data .
ENDMETHOD .
ENDCLASS .
START-OF-SELECTION .
DATA : ls_data1 TYPE lcl_test_ref=> ty_ref ,
ls_data2 TYPE lcl_test_ref =>ty_ref .
ls_data1 = lcl_test_ref=> test_get_ref_to ( ).
ls_data2 = lcl_test_ref=> test_create_ref ( ).
IF ls_data1- ref_data IS BOUND.
WRITE : / 'Return with statement: GET REFERENCE is bound' .
ELSE .
WRITE : / 'Return with statement: GET REFERENCE is NOT bound' .
ENDIF .
IF ls_data2- ref_data IS BOUND.
WRITE : / 'Return with statement: CREATE DATA is bound' .
ELSE .
WRITE : / 'Return with statement: CREATE DATA is NOT bound' .
ENDIF .
running this program and you can get the result like this:
I just wander what the difference between these two ways of data reference, any body have any ideal about this?
‎2014 Jun 05 6:02 AM
Both behavior can be found in keyword documentation.
First case documentation says:
If you place a reference to a local field in a procedure in a global reference variable, the reference will become invalid when you leave the procedure. You cannot then dereference the reference variable.
Second case documentation says:
The CREATE DATA statement creates an anonymous data object and assigns the reference to the data object of the dref reference variables.
By default, the data object is created in the internal session of the current program and remains there for as long as it is required. If it is no longer referenced by reference variables, it is deleted by the garbage collector.
‎2014 Jun 05 6:02 AM
Both behavior can be found in keyword documentation.
First case documentation says:
If you place a reference to a local field in a procedure in a global reference variable, the reference will become invalid when you leave the procedure. You cannot then dereference the reference variable.
Second case documentation says:
The CREATE DATA statement creates an anonymous data object and assigns the reference to the data object of the dref reference variables.
By default, the data object is created in the internal session of the current program and remains there for as long as it is required. If it is no longer referenced by reference variables, it is deleted by the garbage collector.
‎2014 Jun 05 7:00 AM
Hi Manish,
Wonderful explanation! it makes me get more clear understanding about data reference in ABAP; thank you very much.