0

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

Techy
  • 315
  • 1
  • 5
  • 14
  • I have read your question 3 times and I'm still confused: what are you asking? The title speaks of synchronization, but the question is about cleaning up. I can't make any sense of "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". Is this a race condition of the first timer going off during clean up or is it just going off because it is being cleaned up? What is this "it" that tries to access the memory? – stefaanv Jun 19 '12 at 15:00
  • Oops...sorry to make this a mess...The logic is simple here..When the first timer gets destroyed all my other timers which are active should also be destroyed..To achieve this should i call the StopTimer of each timer that i have created?? – Techy Jun 20 '12 at 04:05

2 Answers2

0

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.

Rook
  • 5,734
  • 3
  • 34
  • 43
0

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.

Rafael Baptista
  • 11,181
  • 5
  • 39
  • 59