How does Visibroker use rules with managing memory and complex data?

From Support
Jump to: navigation, search

Question:

How does Visibroker use rules with managing memory and complex data structures?

Answer:

An example with a description of how VisiBroker handles memory is provided in the resolution below.
Further reading on the subject is in the  VisiBroker for C++ Programmer's Guide  Version 3.2, Chapter 9, Parameter  Passing Rules fro the IDL to C++  Mapping.

Example of variable-length struct:



If you have an idl interface which looks like:

  struct myStruct {
    string id;
    float  value;
  };

  myStruct getStruct();
  void getInOutStruct(inout myStruct value);
  void getOutStruct(out myStruct value);

How is memory management handled in this case?  A general rule to follow is that
memory management rules apply not only to the top level value you're passing but
also any elements or element's elements, etc. Therefore, below are a few examples
to illustrate this.

Return value:



If we want to pass this struct from the server to the client as a return the
server would look like:

 
  myStruct* myImpl::getStruct()
  {
    myStruct* newStruct = new myStruct;
    newStruct->id = CORBA::String_dup("hello there");
    newStruct->value = 10.5;

    return newStruct;
  }

the client would look like:
 
  myStruct_var value;
  value = getStruct();
  cout << "id from server: " << value->id << endl;
  cout << "value from server: " << value->value << endl;
 

The orb will return this to the client. On the server side the orb will also
delete the data returned by the function.

InOut value:



For an inout the client would look like:

  myStruct_var struct_value = new myStruct;
  struct_value->id = CORBA::string_dup("hello there");
  struct_value->value = 1.1;
  getInOutStruct(struct_value.inout());
  cout << "returned string is: " << struct_value->id;
  cout << "returned float is: " << struct_value->value;

The client will call getInOutStruct() passing the contents of "myStruct" to
the server. The server will read these out, replace them, and pass it back. The
results will be in myStruct_var.

The server would look like:

  void myImpl::getInOutStruct(myStruct& value)
  {
    cout << "I got passed from the client: " << endl;
    cout << "  " << value.id << endl;
    cout << "  " << value.value << endl;

    // now replace values to send them back
    value.id = CORBA::string_dup("Hi!");
    value.value = 100.5;
  }
 

Out value:



For an out value the client would look like:

  myStruct_var get_value;
  getOutStruct(get_value.out());
 

The server would look like:

  void myImpl::getOut(myStruct_ptr& value)
  {
    value = new myStruct;
    value->id = CORBA::string_dup("hello from the server");
    value->value = 50.6;
  }

 



Article originally contributed by Borland Developer Support