Discussion:
[omniORB] Memory management questions in ORB
Luo Yongheng
2008-04-05 21:54:36 UTC
Permalink
Hi,

I did not find explanations on how to manage memory allocation/release with pointer types in C++ mapping specification.

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?

Thanks.

Thierry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.omniorb-support.com/pipermail/omniorb-list/attachments/20080405/204f6a3e/attachment.htm
Bruce Visscher
2008-04-06 06:31:35 UTC
Permalink
Thierry,
Post by Luo Yongheng
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

Continue reading on narkive:
Loading...