problem with MMTimer [maybe OT]

W

Wasabi

Hi,

maybe OT...
I'm writing a windowless sw, I need a timer for each object that call a
non-static function ( myFunc() ) and refresh a value ( myValue ).

I've wrote this code:


class myClass {

public:

int myValue;

static void CALLBACK myClass::TimeProc(UINT uID,UINT uMsg,DWORD
dwUser,DWORD dw1,DWORD dw2) {
reinterpret_cast<myClass*>(dw1)->myFunc();

}

void myClass::myFunc () {
printf ("!! myFunc() !!");
myValue = 100;
}

void myClass::startTimer() {
MMRESULT myTimer =
timeSetEvent(1000,0,TimeProc,reinterpret_cast<DWORD_PTR>(this),TIME_PERIODIC|TIME_CALLBACK_FUNCTION);
}

};

I've tryed to pass a class pointer to the static callback function.
myFunc () is called, but when I try to change myValue it returns
NullPointerException error

any ideas ???


ps: sorry for my horrible english :)
 
V

Victor Bazarov

Wasabi said:
I'm writing a windowless sw, I need a timer for each object that call
a non-static function ( myFunc() ) and refresh a value ( myValue ).

I've wrote this code:


class myClass {

public:

int myValue;

static void CALLBACK myClass::TimeProc(UINT uID,UINT uMsg,DWORD

Please do not use qualified names when declaring members inside the class
definition (just drop the "myClass::" here). Reduce the clutter!
dwUser,DWORD dw1,DWORD dw2) {
reinterpret_cast<myClass*>(dw1)->myFunc();

You cast your 'dw1' to be the object, but never check that what you got is
actually a non-null pointer before using it. Do check. Or assert.
}

void myClass::myFunc () {
printf ("!! myFunc() !!");
myValue = 100;
}

void myClass::startTimer() {
MMRESULT myTimer =
timeSetEvent(1000,0,TimeProc,reinterpret_cast<DWORD_PTR>(this),TIME_PERIODIC|TIME_CALLBACK_FUNCTION);
}

};

I've tryed to pass a class pointer to the static callback function.
myFunc () is called, but when I try to change myValue it returns
NullPointerException error

any ideas ???

You don't show us how 'startTimer' is called, and for what object. It
can very well be that the object for which you call 'startTimer' is dead
by the time the timer starts ticking (and calling the function).

When the timer ticks, is it the same thread or different? If it's anoter
thread, you cannot allow it to use automatic objects, different threads
use different automatic object storage areas (stacks), most likely. You
need to call 'startTimer' for an object created dynamically (via 'new').

Of course, neither timers, nor threads, are topical here. You would be
much better off in the newsgroup that deals with your OS.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top