2 * @file Bug_2820_Regression_Test.cpp
4 * Verify that the event handler reference counting works correctly
5 * when the reactor is destroyed.
7 * Pushing a notification through the reactor increments the reference
8 * count on the target event handler. Both dispatching and purging
9 * the notification decrement the reference count. However,
10 * destroying the reactor used to not decrement the reference count.
11 * This test reproduces the problem and serves as a regression for it.
13 * @author Carlos O'Ryan <coryan@atdesk.com>
16 #include "test_config.h"
18 #include "ace/Reactor.h"
19 #include "ace/Select_Reactor.h"
22 * @class Simple_Handler
24 * @brief A simple event handler for the test
26 class Simple_Handler
: public ACE_Event_Handler
30 Simple_Handler(ACE_Reactor
* reactor
);
33 ~Simple_Handler() override
;
35 /// Receive (and ignore) the notifications
36 int handle_exception(ACE_HANDLE
) override
;
40 run_main (int, ACE_TCHAR
*[])
42 ACE_START_TEST (ACE_TEXT ("Bug_2820_Regression_Test"));
46 std::unique_ptr
<ACE_Reactor
> reactor(new ACE_Reactor(new ACE_Select_Reactor
, 1));
47 ACE_Event_Handler_var v
=
48 ACE::make_event_handler
<Simple_Handler
> (reactor
.get());
50 ACE_Event_Handler::Reference_Count pre_notify_count
=
53 int const notify_count
= 4;
54 for(int i
= 0; i
!= notify_count
; ++i
)
56 reactor
->notify(v
.handler());
59 ACE_Event_Handler::Reference_Count pos_notify_count
=
62 if(pos_notify_count
!= pre_notify_count
+ notify_count
+ 1)
66 ACE_TEXT("Reference count should increase by %d.")
67 ACE_TEXT(" Initial count=%d, final count = %d\n"),
68 notify_count
, pre_notify_count
, pos_notify_count
));
73 // Reset the reactor in the event handler, since it is gone.p
76 ACE_Event_Handler::Reference_Count pos_release_count
=
79 // Only our explicit calls to add_reference() should be reflected in
80 // the refence_count...
81 if (pos_release_count
!= pre_notify_count
+ 2)
85 ACE_TEXT("Reference count should have increased by 2.")
86 ACE_TEXT(" Initial count=%d, final count = %d\n"),
87 pre_notify_count
, pos_release_count
));
91 ACE_TEXT("Ref count results. pre_notify refcount=%d,")
92 ACE_TEXT(" pos_notify=%d, pos_delete=%d\n"),
93 pre_notify_count
, pos_notify_count
, pos_release_count
));
95 // Remove a reference for each time we explicitly increased it.
96 v
->remove_reference();
97 v
->remove_reference();
98 ACE_Event_Handler::Reference_Count pos_remove_count
=
99 v
->remove_reference();
102 ACE_TEXT("Ref count results. pre_notify refcount=%d,")
103 ACE_TEXT(" pos_notify=%d, pos_delete=%d, pos_remove=%d\n"),
104 pre_notify_count
, pos_notify_count
, pos_release_count
,
112 // ============================================
117 : ACE_Event_Handler(r
)
119 reference_counting_policy().value(
120 ACE_Event_Handler::Reference_Counting_Policy::ENABLED
);
129 handle_exception(ACE_HANDLE
)