Threads and memory

Hello all!

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:

/** Brownian Motion 2015
* date: 200615
* file: alarmclock_dev.cpp
* description: DEVELOPMENT alarm clock*/

/* Faulting application name: AlarmClock.exe, version: 0.0.0.0, time stamp: 0x559a8824
Faulting module name: ntdll.dll, version: 6.1.7601.18869, time stamp: 0x55636317
Exception code: 0xc0000005
Fault offset: 0x0002e3fe
Faulting process id: 0x1d10

Faulting application path: D:\Coding\CodeBlocksProjects\SFML_Apps\Alarm Clock\bin\Release\AlarmClock.exe
Faulting module path: C:\Windows\SysWOW64\ntdll.dll
Report Id: 525672de-2451-11e5-ae9c-902b343fb918
[debug] 14 Thread 5736.0x1bd8 0x77069d5f in ntdll!RtlLookupAtomInAtomTable () from C:\Windows\system32\ntdll.dll
[debug] 12 Thread 5736.0x10c8 0x77051f92 in ntdll!LdrQueryProcessModuleInformation () from C:\Windows\system32\ntdll.dll
[debug] 11 Thread 5736.0x1854 0x7708b061 in ntdll!CsrAllocateMessagePointer () from C:\Windows\system32\ntdll.dll
[debug] 9 Thread 5736.0x7d8 0x7704f91d in ntdll!RtlUpdateClonedSRWLock () from C:\Windows\system32\ntdll.dll
[debug] 7 Thread 5736.0xd0c 0x770501a9 in ntdll!RtlEnableEarlyCriticalSectionEventCreation () from C:\Windows\system32\ntdll.dll
[debug] 3 Thread 5736.0x1498 0x770501a9 in ntdll!RtlEnableEarlyCriticalSectionEventCreation () from C:\Windows\system32\ntdll.dll
[debug]* 1 Thread 5736.0x198c alarmDev () at D:\Coding\CodeBlocksProjects\SFML_Apps\Alarm Clock\alarmclock_dev.cpp:192..

*/

#include "SFML_header.hpp"

bool ringalarm;
bool runtimer = false;
void timer(Time&);
void alarm();
Time timerclock;

void alarmDev()
{
sf::RenderWindow window;
float x_scr = 225, y_scr = 110;
window.create(sf::VideoMode( x_scr, y_scr), "TOX");
window.setFramerateLimit(30);

/** ........ */

int timermode = 0;

timerclock.setTime(0,0,0);
/** Begin main loop

while (window.isOpen())
{ /** ........ */

window.draw(timerSet);
window.display();
//}

//!< 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!



Topic archived. No new replies allowed.