Advertisement
rplantiko

Event registry prevents object recycling

Jun 7th, 2019
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 1.34 KB | None | 0 0
  1. *&---------------------------------------------------------------------*
  2. *& Report ZZ_TEST_OBJ_EVT_LIFESPAN
  3. *& Registering an object for an event prevents it from being recycled
  4. *&---------------------------------------------------------------------*
  5. *& If you run into this problem: do not register "for all instances",
  6. *& but for particular instances. The handler will then be recycled
  7. *& together with the event triggerer, which in many cases solves the
  8. *& problem.
  9. *&---------------------------------------------------------------------*
  10. REPORT ZZ_TEST_OBJ_EVT_LIFESPAN.
  11.  
  12. class a definition.
  13.   public section.
  14.     events: e.
  15.     methods: raise.
  16. endclass.
  17.  
  18. class b definition.
  19.   public section.
  20.     methods:
  21.       m for event e of a,
  22.       constructor.
  23. endclass.
  24.  
  25. class a implementation.
  26.   method raise.
  27.     raise event e.
  28.   endmethod.
  29. endclass.
  30.  
  31. class b implementation.
  32.   method m.
  33.     write: / 'Handler called'.
  34.   endmethod.
  35.   method constructor.
  36.     set handler m for all instances.
  37.   endmethod.
  38. endclass.
  39.  
  40. start-of-selection.
  41.   data(go_a) = new a( ).
  42.   perform start.
  43.  
  44. form start.
  45.   write: / 'First call'.
  46.   perform test. " -> writes 'Handler called'
  47.  
  48.   write: / 'Second call'.
  49.   perform test. " -> 'Handler called' will be written twice
  50. endform.
  51.  
  52. form test.
  53.   data(lo_b) = new b( ).
  54.   go_a->raise( ).
  55. endform.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement