[omniORB] CORBA::string_alloc(len) problem

Bruce Visscher visschb@rjrt.com
Thu, 15 Jul 1999 11:57:46 -0400


Just a nit.

Stephen Coy wrote:
> 
> You probably need to do something like:
> 
>     char * myStr = CORBA::string_alloc(100);
>     String_var myStrVar = myStr;

I assume you meant CORBA::String_var.

Please don't do this.  This is copy initialization.  According to the
ISO C++ standard this is defined to create a temporary using the
converting constructor, CORBA::String_var::String_var(char*), then to
invoke the copy constructor from the temporary and finally to invoke the
String_var destructor on the temporary.  This winds up doing a strlen on
unitialized memory in the copy constructor.

Compilers are allowed to optimize away the temporary.  Most will. 
However, the DEC C++ compiler on the VAX won't (it always does on Alpha
VMS;  I assume it does on Tru64 Unix).  Therefore, you will only have
problems on certain platforms (maybe just the VAX?).  But if you really
want to be portable you should use direct initialization syntax:

	CORBA::String_var myStrVar(myStr);

I realize there are even examples using copy initialization syntax like
the above in the CORBA standard.  IMHO, the (CORBA) standard is wrong.

Bruce Visscher