[omniORB] FW: Sequence of Strings

David Riddoch djr@orl.co.uk
Tue, 24 Nov 1998 17:09:48 +0000 (GMT)


Tom,

Your code is correct - and should have worked. What compiler and version
of omniORB are you using?


When you assign a value to a CORBA string, the value is either copied or
consumed. If you pass a (const char*) it will be copied. If you pass a
(char*) it is consumed - ie. the CORBA string takes over management of the
storage, which MUST have been allocated using CORBA::string_***

The following fails because string constants have type (char*), and so the
string is assumed to be allocated by CORBA::string_*** and is consumed.

>   myStringSeq strSeq;
>   strSeq.length(1);
>   strSeq[0] = "hello";
>   strSeq[0] = "world";	// crashes here

To correct this write
  strSeq[0] = (const char*) "hello";
or
  strSeq[0] = CORBA::string_dup("hello");

If you do something like ...
  const char* pStr = CORBA::string_dup("test");
  strSeq[0] = pStr;
then the string will be copied twice and you'll probably get a memory 
leak.

Hope this helps,
David