Archived

This forum has been archived. Please start a new discussion on GitHub.

Ice AMI Callback Object Lifetime

A call to an AMI method requires a callback object that implements the output side of the AMI request. Easy enough. The example on page 870 in the manual looks like this:
Demo::ModelPrx model = ...;
AMI_Model_interpolatePtr cb = new AMI_Model_interpolateI;
Demo::Grid grid;
initializeGrid(grid);
model->interpolate_async(cb, grid, 0.5);

The AMI_Model_interpolatePtr variable cb will fall out of scope at the end of the routine and be destroyed. Since the callback object pointer is passed into the _async request and it holds a pointer to the callback object instance, so will the callback object itself will be automatically destroyed by the Ice runtime after ice_response or ice_exception has been called?

In section 31.3.4 of the manual (Concurrency Issues), the first bullet point states that a callback object must not be used for multiple simultaneous invocations, and I'm guessing this is because one thread could destroy the callback object before the second one called it?

TIA

Comments

  • bernard
    bernard Jupiter, FL
    Hi Eric,

    The callback is ref-counted and gets deleted when the ref-count reaches 0, typically after ice_response() or ice_exception() has been called. You don't need to do anything special.

    Regarding concurrent use of a callback, the issue is not lifetime: the ref-counting of the callback is thread-safe. These callback objects are just not designed to handle multiple concurrent callbacks.

    Best regards,
    Bernard
  • Thanks.

    Thanks bernard!