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"
17 #include "ace/Auto_Ptr.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
);
35 /// Receive (and ignore) the notifications
36 virtual int handle_exception(ACE_HANDLE
);
40 run_main (int, ACE_TCHAR
*[])
42 ACE_START_TEST (ACE_TEXT ("Bug_2820_Regression_Test"));
46 #if defined ACE_HAS_CPP11
47 std::unique_ptr
<ACE_Reactor
> reactor(new ACE_Reactor(new ACE_Select_Reactor
, 1));
48 ACE_Event_Handler_var v
=
49 ACE::make_event_handler
<Simple_Handler
> (reactor
.get());
51 auto_ptr
<ACE_Reactor
> reactor(new ACE_Reactor(new ACE_Select_Reactor
, 1));
52 ACE_Event_Handler_var
v(
53 new Simple_Handler(reactor
.get()));
56 ACE_Event_Handler::Reference_Count pre_notify_count
=
59 int const notify_count
= 4;
60 for(int i
= 0; i
!= notify_count
; ++i
)
62 reactor
->notify(v
.handler());
65 ACE_Event_Handler::Reference_Count pos_notify_count
=
68 if(pos_notify_count
!= pre_notify_count
+ notify_count
+ 1)
72 ACE_TEXT("Reference count should increase by %d.")
73 ACE_TEXT(" Initial count=%d, final count = %d\n"),
74 notify_count
, pre_notify_count
, pos_notify_count
));
77 ACE_auto_ptr_reset(reactor
, (ACE_Reactor
*)0);
79 // Reset the reactor in the event handler, since it is gone.p
82 ACE_Event_Handler::Reference_Count pos_release_count
=
85 // Only our explicit calls to add_reference() should be reflected in
86 // the refence_count...
87 if (pos_release_count
!= pre_notify_count
+ 2)
91 ACE_TEXT("Reference count should have increased by 2.")
92 ACE_TEXT(" Initial count=%d, final count = %d\n"),
93 pre_notify_count
, pos_release_count
));
97 ACE_TEXT("Ref count results. pre_notify refcount=%d,")
98 ACE_TEXT(" pos_notify=%d, pos_delete=%d\n"),
99 pre_notify_count
, pos_notify_count
, pos_release_count
));
101 // Remove a reference for each time we explicitly increased it.
102 v
->remove_reference();
103 v
->remove_reference();
104 ACE_Event_Handler::Reference_Count pos_remove_count
=
105 v
->remove_reference();
108 ACE_TEXT("Ref count results. pre_notify refcount=%d,")
109 ACE_TEXT(" pos_notify=%d, pos_delete=%d, pos_remove=%d\n"),
110 pre_notify_count
, pos_notify_count
, pos_release_count
,
118 // ============================================
123 : ACE_Event_Handler(r
)
125 reference_counting_policy().value(
126 ACE_Event_Handler::Reference_Counting_Policy::ENABLED
);
135 handle_exception(ACE_HANDLE
)