0

I am trying to make a Clock with the timeGetTime function and some others. But I keep getting an exception error. I know mayby the quality of the program is not really good, but I was just trying to get it work. It's supposed to be a singleton. I hope you can help me!

// The Clock header file

// The Clock API for meassuring time.
#include<Windows.h>
#include<WinBase.h>
#include<MMSystem.h>

class cTime
{
private:
    double m_Ns;
    double m_Ms;
    double m_S;
public:

    // Set Values
    void SetN(double N) {m_Ns = N;}
    void SetM(double M) {m_Ns = M;}
    void SetS(double S) {m_Ns = S;}

    // Get Values
    double GetN() {return m_Ns;}
    double GetM() {return m_Ms;}
    double GetS() {return m_S;}


// GetTime functions
//int GetDiffrenceNs();
//int GetDiffrenceMs();
//int GetDiffrenceS();
};

class cClock
{
private:
cTime m_CurrentTime;            // CurrentTime object
static cClock* m_pClock;                // Pointer to only instance
cTime m_LastTime;               // LastTime object

bool m_PerformanceCounter;      // Set to true if the performance counter         is available
double m_Frequency;         // Tells the frequenty of the     PerformanceCounter. The value that the PerformanceCounter will increase each second.
double m_CounterTime;           // The Counter of the PerformanceCounter.
double m_Trillingstijd;         // How long one count of the performance counter will take.

public:

static cClock* GetClock();

cTime CurrentTime(); // Get the CurrentTime.
cTime LastTime(); // Get the LastTime.

// Virtual destructor.
virtual ~cClock();

protected:
// Protected constructor.
cClock();
};

// The clock cpp file

#include "Clock.h"


cClock* cClock::m_pClock = 0;

cClock* cClock::GetClock()
{

//BOOL perf_flag;        // Timer Selection Flag
//double time_factor;    // Time Scaling Factor
//LONGLONG last_time;    // Previous timer value
//LONGLONG perf_cnt;

if (QueryPerformanceFrequency((LARGE_INTEGER *) &m_pClock->m_Frequency)) 
    {

    QueryPerformanceCounter((LARGE_INTEGER *) &m_pClock->m_CounterTime);

    m_pClock->m_PerformanceCounter = true;
    m_pClock->m_Trillingstijd=1.0/m_pClock->m_Frequency;

    double LastedSeconds = m_pClock->m_CounterTime/m_pClock->m_Frequency;

    m_pClock->m_LastTime.SetN(LastedSeconds*1000000);
    m_pClock->m_LastTime.SetM(LastedSeconds*1000);
    m_pClock->m_LastTime.SetS(LastedSeconds);

} 

else 
{
    m_pClock->m_PerformanceCounter = false;

    double LastedMiliseconds = timeGetTime();

    m_pClock->m_LastTime.SetN(LastedMiliseconds*1000);
    m_pClock->m_LastTime.SetM(LastedMiliseconds);
    m_pClock->m_LastTime.SetS(LastedMiliseconds/1000);
}

return cClock::m_pClock;
}

cTime cClock::LastTime()
{
    return m_LastTime;
}

cTime cClock::CurrentTime()
{
if(m_PerformanceCounter)
{
    QueryPerformanceCounter((LARGE_INTEGER *) &m_CounterTime);

    double LastedSeconds = m_CounterTime/m_Frequency;

    m_CurrentTime.SetN(LastedSeconds*1000000);
    m_CurrentTime.SetM(LastedSeconds*1000);
    m_CurrentTime.SetS(LastedSeconds);
}
else 
{
    int LastedMiliseconds = timeGetTime();

    m_CurrentTime.SetN(LastedMiliseconds*1000);
    m_CurrentTime.SetM(LastedMiliseconds);
    m_CurrentTime.SetS(LastedMiliseconds/1000);
}

m_LastTime = m_CurrentTime;

return m_CurrentTime;

}

This is my main, really simple but I just tried to get it to work but it doesn't...

#include "Clock.h"
#include<iostream>

using namespace std;

int main()
{
cClock* Clock = cClock::GetClock();
cTime Test = Clock->CurrentTime();

cout << Test.GetN();
cout << Test.GetM();
cout << Test.GetS();
int temp;
cin >> temp;
return 0;
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 3
    Off-topic: That's a huge amount of code for a simple clock. If you have a modern C++, you can use the portable high-resolution clock from ``, [see this answer](http://stackoverflow.com/questions/7056619/how-to-insert-a-countdown-timer-in-a-quiz-program-that-is-created-using-c/7056662#7056662) for an example. – Kerrek SB Aug 14 '11 at 15:33

2 Answers2

2

It seems that the method cClock* cClock::GetClock() uses m_pClock without initializing it (it is still 0).

Jiri Kriz
  • 9,192
  • 3
  • 29
  • 36
1

You are never creating an instance of cClock, just accessing a null pointer to one.

If you really think a singleton is a good idea, then cClock::GetClock() will have to create one if it doesn't already exist; along the lines of

cClock* cClock::GetClock()
{
    if (!m_pClock) {
        m_pClock = new cClock;
    }

    // remainder of function

    return m_pClock;
}

Note that this isn't thread-safe, and also introduces a memory leak. Singletons are difficult to implement in C++, and best avoided unless there is a genuine reason for wanting one. I would move the logic of GetClock() into a public constructor, and allow client code to create and destroy clock objects as it sees fit.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644