Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / POA / Reference_Counting / test.cpp
blobcc82214a1fb26bdf180f4bcb7586fa16cc868c69
1 #include "HelloS.h"
3 class Hello_impl :
4 virtual public POA_Hello
6 public:
7 Hello_impl ()
9 ACE_DEBUG ((LM_DEBUG, "Hello_impl::Hello_impl()\n"));
12 ~Hello_impl ()
14 ACE_DEBUG ((LM_DEBUG, "Hello_impl::~Hello_impl()\n"));
17 virtual void moo (
23 CORBA::ULong
24 getRefCount (PortableServer::ServantBase * sb)
26 return sb->_refcount_value ();
29 int
30 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
32 try
34 CORBA::ORB_var orb =
35 CORBA::ORB_init (argc, argv);
37 CORBA::Object_var poa_object =
38 orb->resolve_initial_references("RootPOA");
40 PortableServer::POA_var poa =
41 PortableServer::POA::_narrow (poa_object.in ());
43 if (CORBA::is_nil (poa.in ()))
44 ACE_ERROR_RETURN ((LM_ERROR,
45 " (%P|%t) Panic: nil RootPOA\n"),
46 1);
48 Hello_impl * h = 0;
49 ACE_NEW_RETURN (h,Hello_impl, 1);
51 CORBA::ULong before_act = h->_refcount_value ();
53 ACE_DEBUG ((LM_DEBUG, "Before activation: %d\n", before_act));
55 PortableServer::ObjectId_var oid = poa->activate_object (h);
57 CORBA::ULong after_act = h->_refcount_value ();
59 ACE_DEBUG ((LM_DEBUG, "After activation: %d\n", after_act));
62 * C++ Language Mapping (formal/03-06-03), section 1.37.3 (Servant
63 * Memory Management Considerations), first bullet on page 1-136:
65 * POA::id_to_servant returns a Servant. The POA invokes _add_ref
66 * once on the Servant before returning it; the caller of
67 * id_to_servant is responsible for invoking _remove_ref on the
68 * returned servant when it is finished with it.
71 CORBA::ULong refCountBeforeIdToServant =
72 h->_refcount_value ();
74 ACE_DEBUG ((LM_DEBUG, "Before id_to_servant: %d\n", refCountBeforeIdToServant));
76 PortableServer::ServantBase_var srv = poa->id_to_servant (oid.in());
78 CORBA::ULong refCountAfterIdToServant =
79 srv->_refcount_value ();
81 ACE_DEBUG ((LM_DEBUG, "After id_to_servant: %d\n", refCountAfterIdToServant));
84 * According to the above quote, this assertion shall be true.
86 ACE_ASSERT (refCountAfterIdToServant == refCountBeforeIdToServant + 1);
89 * At the end of this scope, "srv" is destructed, which decrements
90 * the servant's reference count.
94 CORBA::ULong before_deact = h->_refcount_value ();
96 ACE_DEBUG ((LM_DEBUG, "Before deactivate_object: %d\n", before_deact));
98 poa->deactivate_object (oid.in());
101 * Because id_to_servant did not increment the reference count, but
102 * the reference count was decremented by the "srv" destructor, the
103 * reference count, using TAO 1.4.5, is now 0, and the servant has
104 * been destructed. So the following will crash, despite being
105 * correct.
108 CORBA::ULong after_deact = h->_refcount_value ();
110 ACE_DEBUG ((LM_DEBUG, "After deactivate_object: %d\n", after_deact));
112 h->_remove_ref ();
114 orb->shutdown (true);
116 orb->destroy ();
118 catch (const CORBA::Exception& ex)
120 ex._tao_print_exception ("Exception caught:");
121 return 1;
124 return 0;