Question

I have a timer class..This timer will be started from various portions of my code blocks..I have another class to manage this timer namely CTimerManager...This is a static class ..So the issue i face here is when i de-initialize my application all my timer needs gets destroyed...But since i have started many timers when the first timer goes off the memory goes NULL and if any other timers are active and it tries to access the memory exception occurs(due to the singletn instance of CTimerManager)... Anyone have an idea of how to face this issue

Was it helpful?

Solution

Only create timer instances via a factory method of you CTimerManager class. These timers are intrinsically bound to your manager, and belong to it, and therefore only the manager should take responsibility for their creation and deletion.

In your manager class' destructor you should halt all your timers and delete them. That way no timer will remain alive or active once the manager has been destroyed.

OTHER TIPS

From your description you are creating a class that holds timers that call back into themselves after a given amount of time. When you destroy the manager, the timer data structures go away and then the timers call back the objects are destroyed.

You have to make sure to turn off all your timers when you call the destructor for CTimerManager.

Are you using threads and then calling sleep? In that case each timer need to periodically check a flag to see if the timer is being terminated early. Then in the destructor you set a flag to terminate the timer early and join() all your timer threads.

If you are calling some operating system timer callback, it probably has a function to cancel a timer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top