470,833 Members | 1,195 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

problem with MMTimer [maybe OT]

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<DWOR D_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 :)
May 30 '06 #1
1 2184
Wasabi wrote:
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<DWOR D_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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Madhu Gopinathan | last post: by
11 posts views Thread by ASP newbie | last post: by
5 posts views Thread by Mark | last post: by
6 posts views Thread by Tom | last post: by
14 posts views Thread by rurpy | last post: by
1 post views Thread by tom.eeraerts | last post: by
4 posts views Thread by Dan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.