Search code examples
c++corba

handling memory for IDL sequence return types


I have an idl definition as follow

typedef sequence<octet> ByteArray;

interface Connection {
    ByteArray get_id ();
}

And there is this client side code

ByteArray * idToEncrypt = connection->get_id();
encryptId(idToEncrypt); 
... // rest of code

The function encryptId() has the signature ByteArray* encryptId(ByteArray* idToEncrypt). I cannot change this signature.

For automatic memory handling, the type of idToEncrypt was changed to ByteArray_var. The problem is I dont know how to get 'ByteArray *' from ByteArray_var and pass it to encryptId().

Is there a way to automatically handle memory allocated "idToEncrypt" and still pass it to encryptId() as "ByteArray *"?


Solution

  • You should look at the _var C++ mapping in the CORBA specs.

    For a sequence type var, I think the best approach might be to use the inout() member, i.e.:

    ByteArray_var idToEncrypt = connection->get_id();
    encryptId(&idToEncrypt.inout()); 
    

    inout returns a non-const reference and you're just taking the address of the underlying object with &.

    Do note: inout does dereference the internal pointer, so it would be illegal to call inout() on a _var that doesn't hold a pointer. However, the call to get_id() must always return a valid pointer, so the code is OK without checking.

    If you need a generic approach where you don't know if the _varis initialized, you could use:

    ByteArray* p = idToEncrypt.operator->();
    

    as operator-> seems to be the only way to directly get at the pointer.