I am developing a simple timer/clock program on Windows 7, Using SFML and Codeblocks with Mingw 4.7.1 compiler and include file "mingw.thread.h" .
The release version runs ok, but crashes after between 2 - 4 hours with an error 0xc0000005 in ntdll.dll. I tracked the app's behaviour in Task Manager and discovered it was creating an enormous number of page faults after 2 hours - > 70,000,000! and rising.
So no wonder it was crashing.I haven't included all the code, but have attached the snippets that seem relevant, also the output from a debug run.
The reason I am using a thread to update the timer is that the clock is updating as well elsewhere in the main thread. Otherwise the timer blocks all activity until it is stopped. Here is the code:
//!< EVENTS
sf::Event event;
while (window.pollEvent(event))
{ switch (event.type)
{
/** ........ */
//!< check for mouse entry
case sf::Event::MouseButtonPressed:
/** ........ */
//!< This if & sub-switch sets the timer on or off
if (event.mouseButton.button == sf::Mouse::Left
&& timerSet.getGlobalBounds().contains(event.mouseButton.x,event.mouseButton.y)
&& clockmode < 1)
{ switch (timermode)
{ case 0:
{ timerSet.changeColour(sf::Color::Red);//!< note thread call is in curly brackets{ }
timermode = 1;
runtimer = true;
thread timerthread(timer,ref(timerclock)); //!< passes a ref to Time& in timer below
timerthread.detach(); //!< note thread call is in curly brackets{ }
break; }
case 1:
timermode = 2;
timerSet.changeColour(sf::Color::Magenta);
runtimer = false;
break;
case 2:
timermode = 0;
timerSet.changeColour(sf::Color::Blue);
timerclock.setTime(0,0,0);
break;
default: break; } } //!< end switch timermode and if
default: break; } //!< end main mouse switch event case
} //!< end of while events
} //!< end of render window
} //!< end main function
//!< timerclock is passed from the thread in timerclock case 0 by ref
//!< and set from here
void timer(Time& ti)
{ time_t start, finish;
int runtime;
time (&start);
while (runtimer) //!< runtimer is a global variable set from timerSet case
{ time (&finish);
runtime = difftime(finish, start);
int hrs = (runtime/3600) % 24;
int mins = ((runtime/60) % 60) % 60;
int secs = (runtime % 3600) % 60;
ti.setTime(hrs,mins,secs); }
}
The header file contains includes such as Time class and other SFML stuff, as well as the mingw.thread.h.
Even without the timer set 'on' the app generates page faults.
Any help would be much appreciated!