Application Development and Automation 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: 
Read only

Free object problem

david_fryda2
Participant
0 Likes
602

Hi everyone,

Here is a code where the object is not really cleared.


CLASS testfree DEFINITION.
  PUBLIC SECTION.
    METHODS set_var.
    METHODS getvar EXPORTING pv_text TYPE char10.
  PRIVATE SECTION.
    CLASS-DATA : gv_text(10).
ENDCLASS.        

CLASS testfree IMPLEMENTATION.
  METHOD set_var.
    gv_text = 'hello world'.
  ENDMETHOD.                    "set_var
  METHOD getvar.
    pv_text =  gv_text.
  ENDMETHOD.                    "getvar

ENDCLASS.                    "testfree IMPLEMENTATION

START-OF-SELECTION.
  DATA: go_testfree TYPE REF TO testfree,
        gv_text1(10).
  CREATE OBJECT go_testfree.
  CALL METHOD go_testfree->set_var.
  CALL METHOD go_testfree->getvar
    IMPORTING
      pv_text = gv_text1.
  WRITE:/ 'before free : ', gv_text1.

  FREE  go_testfree .
  CLEAR gv_text1.

  CREATE OBJECT go_testfree.
  CALL METHOD go_testfree->getvar
    IMPORTING
      pv_text = gv_text1.
  WRITE:/ 'After free : ', gv_text1.


The problem is that gv_text1 is still set to value 'hello world' after the FREE command.
How is it possible ?

Thanks
Regards.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
508

I guess declaring it as CLASS-DATA is the problem here

declare that as DATA

DATA : gv_text(10).

3 REPLIES 3
Read only

Former Member
0 Likes
509

I guess declaring it as CLASS-DATA is the problem here

declare that as DATA

DATA : gv_text(10).

Read only

0 Likes
508

Thanks for the quick and right answer!!!

Regards.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
508

The reason is simply, you have declare the GV_TEXT attribute as a static attribute. You did this using the CLASS-DATA statement, if you want it to be an instance attribute, you need to define with a DATA statement instead.

  private section.
     data: gv_text(10).

Regards,

RIch Heilman