Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Probles on data reference/field-symbols and FREED STACK on OOP program

former_member249594
Participant
0 Kudos

Hi Guru,

I have develop a class on my program and I am confused by some problem on data reference and field-symbols.

Below is my Example program cods:

Main program for calling Class


*&---------------------------------------------------------------------*
*& Report  ZTESTING
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ztesting.
DATA: BEGIN OF ls_line,
      fielda TYPE c,
      fieldb TYPE c,
      END OF ls_line.
DATA: itab LIKE STANDARD TABLE OF ls_line.
DATA: ref TYPE REF TO data.
DATA: o_instance TYPE REF TO ztest_cl.
FIELD-SYMBOLS: <ft> TYPE STANDARD TABLE.

START-OF-SELECTION.
  ls_line-fielda = 'C'.
  ls_line-fielda = 'D'.
  APPEND ls_line TO itab.

  CREATE OBJECT o_instance
         EXPORTING
            i_condition = 'U'.
  CALL METHOD o_instance->get_reference
    EXPORTING
      itab   = itab
    IMPORTING
      ip_ref = ref.
*  ASSIGN ref->* TO <ft>.
*  CLEAR <ft>.
  CALL METHOD o_instance->clear_data
    CHANGING
      cp_ref = ref.
  break chengr.

Cods of Class - ZTEST_CL

Private Section of Class


*"* private components of class ZTEST_CL
*"* do not include other source files here!!!
PRIVATE SECTION.

TYPES:
  BEGIN OF type_line_l,
       field1(1) TYPE c,
       field2(1) TYPE c,
       END OF type_line_l .
TYPES:
  type_itab_l TYPE STANDARD TABLE OF type_line_l .
TYPES:
  BEGIN OF type_line_u,
       field1(1) TYPE c,
       field2(1) TYPE c,
       END OF type_line_u .
TYPES:
  type_itab_u TYPE STANDARD TABLE OF type_line_u .

DATA line_u TYPE type_line_u .
DATA line_l TYPE type_line_l .
DATA itab_u TYPE type_itab_u .
DATA itab_l TYPE type_itab_l .
DATA itab_ref TYPE REF TO data .

METHODS clear_internal_data .

Public Section of Class


*---------------------------------------------------------------------*
*       CLASS ZTEST_CL  DEFINITIO
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS ztest_cl DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

*"* public components of class ZTEST_CL
*"* do not include other source files here!!!
PUBLIC SECTION.

  METHODS constructor
    IMPORTING
      !i_condition TYPE c .
  METHODS get_reference
    IMPORTING
      !itab TYPE STANDARD TABLE
    EXPORTING
      !ip_ref TYPE REF TO data .
  METHODS clear_external_data
    CHANGING
      !cp_ref TYPE REF TO data .
  METHODS clear_data
    CHANGING
      !cp_ref TYPE REF TO data .

Codes of Constructor


METHOD constructor.
***********Importing parameters:
***************I_CONDITION	TYPE C	

  line_u-field1 = 'A'.
  line_u-field2 = 'B'.
  APPEND line_u TO itab_u.

  line_u-field1 = 'X'.
  line_u-field2 = 'Y'.
  APPEND line_u TO itab_u.

  line_l-field1 = 'a'.
  line_l-field2 = 'b'.
  APPEND line_l TO itab_l.

  line_l-field1 = 'a'.
  line_l-field2 = 'b'.
  APPEND line_l TO itab_l.

  CASE i_condition.
    WHEN 'U'.
********Upper Case
*      GET REFERENCE OF itab_u INTO itab_ref.
      CALL METHOD me->get_reference
        EXPORTING
          itab   = itab_u
        IMPORTING
          ip_ref = itab_ref.
    WHEN 'L'.
*********Low Case
*      GET REFERENCE OF itab_l INTO itab_ref.
      CALL METHOD me->get_reference
        EXPORTING
          itab   = itab_l
        IMPORTING
          ip_ref = itab_ref.
  ENDCASE.
ENDMETHOD.                    "constructor

Codes of Method Get_reference


METHOD get_reference.
***********Importing parameters
*************ITAB	TYPE STANDARD TABLE
***********Exporting parameters
**************IP_REF	TYPE REF TO DATA
  GET REFERENCE OF itab INTO ip_ref.
ENDMETHOD.

Codes of Method clear_external_data


METHOD clear_external_data.
*************Changing Parameter
*****************CP_REF	TYPE REF TO DATA	
  FIELD-SYMBOLS: <ft> TYPE STANDARD TABLE.
  ASSIGN cp_ref->* TO <ft>.
*  CHECK sy-subrc EQ 0.
  CLEAR <ft>.
ENDMETHOD.

Codes of Method clear_data


METHOD clear_data.
*************Changing Parameter
*****************CP_REF	TYPE REF TO DATA	
  CALL METHOD me->clear_internal_data.
  CALL METHOD me->clear_external_data
    CHANGING
      cp_ref = cp_ref.
ENDMETHOD.

Codes of Method clear_internal_data


METHOD clear_internal_data .
****************Import parameters
****************Export parameters
  FIELD-SYMBOLS: <ft> TYPE STANDARD TABLE.
  ASSIGN itab_ref->* TO <ft>.
*  CHECK sy-subrc EQ 0.
  clear <ft>.
ENDMETHOD.

My problems are:

1) When I call the method of GET_REFERENCE and get a runtime error on the below statement of private method CLEAR_INTERNAL_DATA:


ASSIGN itab_ref->* TO <ft>.

The runtime analysis tell me that field-symbols <FT> is a protected against. I do not know why this object are protected since it point to private attribute of itab_ref-. How I can change it into un-protected??

2)If I set the parameter ITAB of method GET_REFERENCE to BY PASSING VALUE, it return the EXPORTING parameter IP_REF to calling program with a data reference(in my calling program, the data ref REF receive the value from this method), When I check the REF in debug mode I found it is a FREED STACK data ref and it can not assign the value to field symbol by below statement:


*  ASSIGN ref->* TO <ft>.

Can any friends can give me some message on above question?? Thanks in advance.

Best Regards

Joe

1 REPLY 1

matt
Active Contributor
0 Kudos

In the method get_reference, you get the reference to an IMPORTING parameter - which is protected. When you try to change its value later, it's still protected - it was created in the context of class. Change the type of parameter for ITAB to CHANGING, and it will work.

matt