[omniORB] any and src/examples/anyExample memory leak ?

David Riddoch djr@uk.research.att.com
Mon, 6 Sep 1999 16:54:56 +0100 (GMT)


On Mon, 6 Sep 1999, HA Quoc Viet wrote:

> Hello :o)
> 
> 
> Does anyone know why a server has to "new" a CORBA::Any before
> sending it while sending an address (on a fixed Any) will
> just crash the server?


Because the ORB 'delete's the returned value once it has marshalled it
onto the wire.  Thus in your first example the ORB tries to call 'delete'
on &toto.  Your second version is correct and there is no memory leak.

This is defined in the spec, or you can find out such info by reading a
book about CORBA programming.


David


> 
> 
> specifically, given this IDL (from omniORB's examples)
> 
> class anyExample_i : public virtual _sk_anyExample {
> public:
>   anyExample_i() { }
>   virtual ~anyExample_i() { }
>   virtual CORBA::Any* testOp(const CORBA::Any& a);
>   CORBA::Any toto;
> };
> 
> 
> the following implementation of testOp will crash the server
> 
> CORBA::Any* 
> anyExample_i::testOp(const CORBA::Any& a) {
> 
> toto <<= (CORBA::ULong) 314; // causes the server to crash
> 
> cout << "Returning Any containing: ULong: 314\n" << endl;
> return &toto;
> }
> 
> 
> while this one, with a "new", will run just fine :
> 
> 
> CORBA::Any* 
> anyExample_i::testOp(const CORBA::Any& a) {
> 
> CORBA::Any * result = new CORBA::Any;
> 
> *result <<= (CORBA::ULong) 314 ;
> 
> cout << "Returning Any containing: ULong: 314\n" << endl;
> return result;
> }
> 
> If using new is the only solution, then I'll wonder about
> memory leaks : there's no (easy) way to know when "result" is
> no longer used to delete it, is there ?
> 
>