[omniORB] Passing strings to functions

Bruce Fountain B_Fountain@motherwell.com.au
Thu, 6 Dec 2001 14:35:42 +0800


David Bellette wrote:
> The example is from the first example in the omniOrb manual,
> so it's frustating the basic examples don't even work.

I don't think you took your example from the omniOrb manual. Your
implementation looks like this:

> char* Echo_i::echoString(const char* inString)
> {
>     // this crashes!
>  
>     CORBA::String_var lRet = inString;          // deep copy
>  
>     //return lRet;              // incorrect - this causes a release of
the returned object's memory 
>     return lRet._retn();  // correct way - free is not called
> }

whereas the example in the manual looks like this:

    char* Echo_i::echoString(const char* mesg)
    {
        return CORBA::string_dup(mesg);
    }

... which you could rewrite as follows:

    char* Echo_i::echoString(const char* mesg)
    {
        char* result = CORBA::string_dup(mesg);
        return result;
    }

Note that the returned value is a char*, not a CORBA::String_var. The
CORBA libraries will free whatever memory your method returns, so you
have to allocate it on the heap and not clean it up. If you assign the
result to a _var variable then the associated memory will be cleaned
up when the local variable goes out of scope. If this code worked with
ORBacus then you were just lucky.

Bruce Fountain
brucef@eudoramail.com