[omniORB] String handling problem with VC7.1
Brian Neal
bgneal at gmail.com
Fri Mar 23 08:05:27 GMT 2007
>"Ecker Severin" <Severin.Ecker at arcs.ac.at> wrote:
>
> I'm wondering if I'm just using CORBA in an incorrect way or if there
> really is an issue but whenever I try to use the following code I get an
> debug assertion failure:
>
> Code I'm trying to get to work:
>
> CosNaming::Name var;
> var.length(1);
> var[0].id = (const char*)"test";
What you are doing seems fine from a CORBA perspective. However I just
wanted to point out that on a modern, conforming C++ compiler, the
const char* cast is not necessary. Under the *old* rules of C++, a
literal of the form "test" would have type char*, so ownership would
have been taken by var[0].id, and the destructor would have tried to
free it (bad). Under modern C++, a literal string like "test" has the
type const char* already, so var[0].id will do a deep copy
(essentially calling string_dup() for you). I have used code like the
above, without the cast, many times on omniORB and gcc without any
problems. I strongly suspect VC7.1 also conforms in this manner, and
the problem you are seeing must be something else.
You can test your compiler's conformance in this regard with a sample
C++ program like this one:
#include <iostream>
#include <ostream>
void f(char *s)
{
std::cout << "non-const: " << s << std::endl;
}
void f(const char* s)
{
std::cout << "const: " << s << std::endl;
}
int main()
{
f("test string");
return 0;
}
Regards,
BN
More information about the omniORB-list
mailing list