corbaout-parameterstao

Memory management with CORBA/TAO out parameters


Lets say I have an IDL function:

void foo(out Data d);

When I inherit from the generated code the signature will look sth like this:

void foo(IDL::Data_out d);

My first question is, what do I have to pass on the client side? I tried:

IDL::Data_out d;
_servantRef->foo(d);

but this doesn't work because Data_out doesn't have a default constructor. I then tried:

IDL::Data* d;
_servantRef->foo(d);

but now the compiler can't cast from IDL::Data* to IDL::Data_out. The following works but looks overcomplicated and thus not correct:

IDL::Data* d(NULL);
IDL::Data_out do(d);
_servantRef->foo(do);

How do I have to proceed from there? During its execution of foo() the servant will at some point allocate a data object like this:

void Servant::foo(IDL::Data_out d)
{
  d = new Data();
}

I will then delete the object after having used it on the client side like this:

IDL::Data* d(NULL);
IDL::Data_out do(d);
_servantRef->foo(do);
delete d;

Is this at least correct by its idea or does this work differently? Would appreciate a little help or pointers to documentation where this is described in a understandable way.


Solution

  • You have to use the _var classes correctly, they are like an auto_ptr and make sure the memory is freed when the _var goes out of scope. The client code should be

    IDL::Data_var d;
    _servantRef->foo (d.out ());
    

    The servant code should be

    void Servant::foo(IDL::Data_out d)
    {
      d = new Data();
    }
    

    The new IDL to C++11 language mapping makes this way easier, there the client code is

    IDL::Data d;
    _servantRef->foo (d);
    

    The servant code is

    void Servant::foo(IDL::Data& d)
    {
      // modify d
    }
    

    See TAOX11 for more details about IDL to C++11.