[omniORB] C++ mapping of IDL struct

Carlos carlos at canama.net
Wed Apr 18 02:07:35 BST 2007


El mar, 17-04-2007 a las 07:18 -0700, Sean Parker escribió:
> 
> Hello -
> 
>   I have a question about the "proper" way to return
> structs from server methods. (I end up with a memory leak,
> but the data gets through fine)
> 
> For example:
> 
>   <IDL code>
>   struct MyStruct
>   {
>       long a;
>       string b;
>   };
>   interface X
>   {
>       MyStruct doit();
>   }
>   <end IDL code>
> 
> Then in C++:
> 
>   <C++ code>
> 
>   MyStruct*
>   X_impl::doit()
>   {
>       MyStruct* ms = new MyStruct();
>       ms->a = 0;
>       ms->b = ::CORBA::string_dup( "hello" );
>       return ms;
>   }
> 
>   <end C++ Code>
> 
> 

I think this code is correct, but perhaps the memory leak is in client
side, in order to manage the return in a correct way:

X_var x_var = ....

MyStruct_var s_var = x_var->doit();

Now the return pointer is owned by s_var, so when s_var gets out of
scope the destructor properly frees the memory.

And in server side perhaps a better implementation is:

MyStruct*
X_impl::doit()
{
	MyStruct_var s_var = new MyStruct();
	s_var->a = 0;
	s_var->b = ::CORBA::string_dup("Hello");
	return s_var._retn();
}

This implementation is better because if inside the method a exception
is raised the destructor of MyStruct_var is called, so you don't get a
memory leak.

Cheers.

Carlos.

> I suppose there are "preferred" ways to do this, so that's
> what I'm asking, what's the preferred (or only) way to do
> this w/o memory leaks? When using valgrind, I always have a
> leak of both the string 'b' and the MyStruct allocation. Is
> there a alloc_buf function I need to use, as in creating
> sequences of structs? (I thought of always returning
> sequences of size one, since I do sequences alot elsewhere
> w/o leaks, but that seems klugy...)
> 
>   Any help appreciated - 
>     Thanks and God Bless
> 
>       Sean  
> 
> P.S. I'm using oo 4;
> 
> P.P.S. the C++ mapping doc from OMG suggestes something
> like a _rtrn() function, as an example for implementations
> to do things, but not sure that omniORB has such a thing...
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> 
> _______________________________________________
> omniORB-list mailing list
> omniORB-list at omniorb-support.com
> http://www.omniorb-support.com/mailman/listinfo/omniorb-list
> 
> 




More information about the omniORB-list mailing list