[omniORB] Memory management questions in ORB

Bruce Visscher bruce.visscher at gmail.com
Sat Apr 5 21:31:45 BST 2008


Thierry,

> If a servant function returns a pointer to a dynamically allocated memory,
> the servant generally allocates a heap memory block and assign it to the
> returned pointer. Then whose responsibility is it to release this heap
> memory block?
> According to the case explanation on returning string type (very similiar to
> pointer type?) in mapping specification, it is the caller's responsibility
> to release the memory. From servant side, the caller is the ORB. So we can
> say it is the ORB's responsibility to release all memories allocated by
> servants? So what routines should I conform to when allocating memory in
> servants?

I think you answered most of your own questions correctly.  Yes, it is the
caller's responsibility to release the allocated storage.  Yes, in the servant's
memory address, the caller would be the ORB.  In general, when returning
memory allocated on the heap from a method, you should always use a _var
type to hold the pointer and use the "_retn()" member function.  Specifically,
in the case of strings, you should use CORBA::String_var to hold the pointer.
If you are using an oldish compiler you should be careful to cast
string literals
to pointers to const char.  You should also never use new directly in
the case of
string.  If you really want to you can use CORBA::String_dup, but I
find this is rarely
needed in practice provided you avoid overloads of CORBA::String_var
ctors and assignment ops that take non-const char*'s (which have
horrid semantics
IMHO).  Other types have their own subtleties.  You should check a
good reference.

I hope that makes some sense.

Bruce



More information about the omniORB-list mailing list