I have an object APIController
that handles an external library. And I would like to access this (unique) controller from different calls of the same function (C-style).
Therefor, I thought about making a global APIController
variable in order to access to the same instance throughout the different calls of my function where the given context_t *context
is different at every call.
At the end, the program calls the close
function for every context
.
APIController *controller = NULL;
void call(context_t *context) /* called with different contexts */
{
if (controller == NULL) controller = new APIController();
controller->instances_counter++;
/* use controller */
controller->use_it();
}
void close(context_t *context)
{
controller->instances_counter--;
if (controller->instances_counter == 0)
delete controller;
}
Is it proper to proceed that way? I feel it is not but I don't see another easy way of doing it.