[Ace-users] [tao-users] Is strdup required for return and out parameters
Christian Fromme
christian at ast.dfs.de
Fri Oct 19 01:08:03 CDT 2007
On 18.10. 17:02, Tommy Persson wrote:
> Chris Cleeland <cleeland at ociweb.com> writes:
>
> > On Oct 18, 2007, at 7:40 AM, Vikram Karandikar wrote:
> >
> > > So either i should use strdup or CORBA::string_dup but stick to one.
> > > am i right?
> >
> > No, you should use only CORBA::string_dup() when duplicating CORBA
> > strings.
> >
> > Consult the Henning & Vinoski book suggested by Christian for details
> > and further information.
>
> I have read the book but totally misssed this. So could you indicate
> where it is discussed or why?
Please see the first mail I wrote in this thread, the chapters are
referenced there.
> In page 147 i says that
>
> char * p = CORBA::string_dup ("Hello");
>
> is correct code but "Hello" is not a CORBA::String. And I routinely do
> things like
>
> return CORBA::string_dup (res.c_str());
>
> So what is the problem with this construction?
Of course, the answer depends on the return type of your function there.
You may want to take a look at the implementation of CORBA::string_dup
in TAO. It simply allocates (char *) memory of the size of the original
string, copies original to newly allocated and returns a char * to the
allocated string. Pretty straightforward and save.
If the function above is expected to return CORBA::String_var, you're
fine. The caller takes ownership of your newly allocated string and
takes care of the de-allocation later. Doing a ._retn() on a
CORBA::String_var would also be fine here.
HTH,
Christian Fromme
More information about the Ace-users
mailing list