Search code examples
c++poco-libraries

How to pass Poco::SharedPtr to a TimerCallback function?


I have created a Timer with Poco following the example:

TimerExample example;
Timer timer(250, 500);
timer.start(TimerCallback<TimerExample>(example, &TimerExample::onTimer));

However I for my case TimerExample need to be a Poco::SharedPtr as

Poco::SharedPtr<TimerExample>TimerExample;

The constructor of TimerCallback takes the following parameters :

TimerCallback(
     C & object,
     Callback method
);

Where C is the template Class <TimerExample> for our case.
I'd like to know how to pass the Poco::SharedPtr to the TimerCallback function in order to be consistent with the bullet from relevant poco guide 030-MemoryManagement.pdf where it is stated that:

Once you use SharedPtr for an object, never work with plain pointers to that object again.


Solution

  • As pointed out in the comment by vencik - you can't. As it currently stands, you can only pass in a reference to the object having the callback as member and you have to make sure it is valid during the lifetime of the TimerCallback instantiation - your callback can be SharedPtr that you can deref to pass in, but the TimerCallback will take its address and will know nothing about its sharing elsewhere.