[omniORB] memory leak in C++ servant

Duncan Grisby dgrisby@uk.research.att.com
Wed, 13 Jun 2001 13:51:43 +0100


On Wednesday 13 June, Lars Immisch wrote:

[...]
> It's also possible that I am doing something wrong, of course.

You're doing something wrong :-) .

Your create() function is

        virtual void create(CORBA::Long tid, FTest::Manager_ptr m)
        {
                Server_i *s = new Server_i(m);

                std::cout << '[' << tid << "] created server\n";

                m->created(tid, s->_this());
        }

_this() returns a new object reference, which you never release. It
looks like you have confused servant reference counts with object
reference reference counts. create() should look like

        virtual void create(CORBA::Long tid, FTest::Manager_ptr m)
        {
                Server_i *s = new Server_i(m);

                std::cout << '[' << tid << "] created server\n";

		// Implicitly activate object and return a new object ref
                Server_var server_obj = s->_this();

		// remove the reference we hold to the servant (the
                // POA then holds the only reference to it)
		s->_remove_ref();		

                m->created(tid, server_obj);

		// The object reference is released when the _var goes
                // out of scope.
        }

Now that we have released the servant reference in create(), there is
no need to call _remove_ref() in destroy().

Cheers,

Duncan.

-- 
 -- Duncan Grisby  \  Research Engineer  --
  -- AT&T Laboratories Cambridge          --
   -- http://www.uk.research.att.com/~dpg1 --