[omniORB] DynArray to Any problem

Francois Laflamme francois.laflamme@voxco.com
Fri, 19 May 2000 10:34:28 -0400


Hello,
 
I trying to create a dynamic structure and send it to a server via a method
with an any variable. Everything is working very fine except that I get a
memory leak, of course I'm in C++.
 
I'm using a CORBA::StructMemberSeq to create the members of the sequence and
Orb->create_struct_tc to create the typecode. I'm also using a
CORBA::NameValuePairSeq to fill the value of the dynamic structure. 
 
My dynamic structure should look like this :
//////////////////////////////////////////////////////////
struct MY_STRUCT 
{
 short MyShort;
 
 long LongArray[27];
};
//////////////////////////////////////////////////////////
 
Here is my idl file: 
//////////////////////////////////////////////////////////
interface DynStructServer
{
 void SendMessage(in any Message);
};
//////////////////////////////////////////////////////////
 
Here is the code :
//////////////////////////////////////////////////////////
   CORBA::StructMemberSeq  *tc_members;
   CORBA::NameValuePairSeq *sq;
   CORBA::DynArray_var   da;
   CORBA::TypeCode_var   tc;
   CORBA::DynStruct_var  ds;

   tc_members = new CORBA::StructMemberSeq;
   tc_members->length(2);
   sq = new CORBA::NameValuePairSeq;
   sq->length(2);
 
   /*Set the 1st element typeCode*/
   (*tc_members)[0].name = (const char*)"MyShort";
   (*tc_members)[0].type = CORBA::TypeCode::_duplicate(CORBA::_tc_short);
   /*Set value of the 1st element*/
   (*sq)[0].value <<= (CORBA::Short)1;
 
   /*Set the 2ieme elementTypeCode */
   (*tc_members)[1].name = (const char*)"LongArray";
   (*tc_members)[1].type = 
 
CORBA::TypeCode::NP_array_tc(27,CORBA::TypeCode::_duplicate(CORBA::_tc_long)
);
 
/*To set the LongArray of the CORBA::NameValuePairSeq I create a
CORBA::DynArray_var with Orb->create_dyn_array passing the typecode created
with
CORBA::TypeCode::NP_array_tc(20,CORBA::TypeCode::_duplicate(CORBA::_tc_long)
) and then I set the value with the DynArray::insert_long() function.*/
 
   /*Create the DynArray for the 2nd element*/
   CORBA::TypeCode_var tcArray;
 
   tcArray =
CORBA::TypeCode::NP_array_tc(27,CORBA::TypeCode::_duplicate(CORBA::_tc_long)
);
   da = Orb->create_dyn_array(tcArray);
 
   /*Set the  DynArray values*/
   for (int I=0; I<27; I++)
    da->insert_long(I);    
 
   printf("da->type()->kind() = %d\n",da->to_any()->type()->kind()); 
   /*output = 20 = tk_array*/
 
   switch (TestVersion)
   {
    case 1 :
    {
     /*Set value of the 2nd element (version 1)*/
     (*sq)[1].value <<= da->to_any(); /*The TypeCode kind will be changed,
why???*/
     printf("(*sq)[1].value.type().kind() =
%d\n",(*sq)[1].value.type()->kind());
     /*output = 11 tk_any*/
     break;
    }
    case 2 :
    {
     /*Set value of the 2nd element (version 2)*/
     (*sq)[1].value = *da->to_any();  /*this is the good way, except that we
get a memory leak!!!*/
     printf("(*sq)[1].value.type().kind() =
%d\n",(*sq)[1].value.type()->kind());
     /*output = 20 = tk_array*/
     break;
    }
   }
 
   /*Create the dynamic structure TypeCode*/
   tc = Orb->create_struct_tc("SomeID","MY_STRUCT",*tc_members);
 
   /*Create the DynStruc from the new TypeCode*/
   ds = Orb->create_dyn_struct(tc);
 
   /*Set the members of my structure*/
   ds->set_members(*sq);
 
   /*Call the Server's function dans send the any*/
   DynStructServerRef->SendMessage(*ds->to_any());
   
   delete tc_members;
   delete sq;

//////////////////////////////////////////////////////////
 
 
To set the value of the NameValuePairSeq with the DynArray_var::to_any()
function. There is 2 ways:
 
1) (*sq)[1].value <<= da->to_any();            // da is my
CORBA::DynArray_var
 
With this way the typecode kind of the any variable is changed from
TypeCode_Array to TypeCode_Base, so the CORBA::DynStruct_var::set_members()
failed with an DynAny_InvalidSeq_InsertToAny exception.
 
2) (*sq)[1].value = *da->to_any();            // da is my
CORBA::DynArray_var
 
With this way CORBA::DynStruct_var::set_members() and the
Server::SendMessage function work fine axcept that we loose memory!
 
I'm using OmniOrb v.2.8 on NT and VC++.
 
Questions:
Is the <<= operator of the "value" (any) work proprely should it change the
TypeCode durring the copy?
Is there a way to delete the "value" and free the memory before coping
another any into it?
Is there another way to set a DynArray members of a DynStruct?
 
Thanks for your help.

Frangois Laflamme 
VOXCO

1134, Ste-Catherine Ouest 
Suite 600 
Montrial, Qc 
H3B 1H4 

EMail:  mailto:francois.laflamme@voxco <mailto:francois.laflamme@voxco>
.com <mailto:francois.laflamme@izusoft.com>  
Tel.:   514-861-9255 
Fax:    514-861-9209