Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Using function pointer in callback function

 
Thread Tools Search this Thread
Old 09-09-2003, 10:02 AM   #1
Default Using function pointer in callback function



Hi,



I am writing an app which encapsulates a multimedia timer. I implemented
a TimerProc as static member function and a static member variable pThis
as pseudo this variable to access in the static TimerProc function.
timeSetEvent uses TimerProc to set the callback function.



m_pCallback is a function that is passed using the
SetTimerCallbackFunction. It's a function that an object owning the
CMMTimer object can pass to the CMMTimer class. What I want is that
every time the TimerProc function is called, the m_pCallback function is
executed, resulting in the object owning the timer processing the timer.



Header file looks like this:



/***************************************/

typedef void (CALLBACK * MMTIMERCALLBACK) (TRIGGER eTrigger, UINT uID,
int iVal);

CMMTimer

{

public:

BOOL Stop();

BOOL Start(UINT uPeriod);

BOOL Initialize(UINT uResolution);

CMMTimer();

virtual ~CMMTimer();

void SetTimerCallbackFunction(MMTIMERCALLBACK pCallback);



private:

MMTIMERCALLBACK m_pCallback;

UINT m_uResolution;

UINT m_uPeriod;

UINT m_uTimerID;

BOOL m_bInitialized;



static CMMTimer *pThis;

static void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser,
DWORD dw1, DWORD dw2);

}



/***************************************/



Parts of cpp file look like this



/***************************************/



CMMTimer* CMMTimer:This = NULL;



void CMMTimer::SetTimerCallbackFunction(MMTIMERCALLBACK pCallback)

{

m_pCallback = pCallback;

}



BOOL CMMTimer::Start(UINT uPeriod)

{

if (!m_bInitialized) return FALSE;

m_uPeriod = uPeriod;

pThis = this;

m_uTimerID =

timeSetEvent(

m_uPeriod,

m_uResolution,

TimerProc,

(DWORD) this,

TIME_PERIODIC );

if(! m_uTimerID)

return FALSE;

else

return TRUE;

}



void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD
dw1, DWORD dw2)

{

// I tried this

CMMTimer * pseudoThis = (CMMTimer *) dwUser;

pseudoThis->m_pCallback(<TIMERT, 0, 0); #error#

// and this

pThis->m_pCallback(<TIMERT, 0, 0); #error#

}



I also tried helper functions.





The problem is that this code compiles perfectly, but when I execute the
code, the debugger shows problems at the lines #error#. m_pCallback
cannot be called, the pointer points at memory address 0xcccccccccccccc



I really would like this problem solved.



Thank you



pvdm





}


--
Posted via http://dbforums.com


pvdm
  Reply With Quote
Old 09-09-2003, 01:26 PM   #2
tom_usenet
 
Posts: n/a
Default Re: Using function pointer in callback function
On Tue, 09 Sep 2003 05:02:58 -0400, pvdm <>
wrote:

>
>Hi,
>
>I am writing an app which encapsulates a multimedia timer. I implemented
>a TimerProc as static member function and a static member variable pThis
>as pseudo this variable to access in the static TimerProc function.


Why do you need pThis? You are passing the this argument to the
callback anyway.

>timeSetEvent uses TimerProc to set the callback function.


>m_pCallback is a function that is passed using the
>SetTimerCallbackFunction. It's a function that an object owning the
>CMMTimer object can pass to the CMMTimer class. What I want is that
>every time the TimerProc function is called, the m_pCallback function is
>executed, resulting in the object owning the timer processing the timer.


Right, sounds reasonable.

>Header file looks like this:
>
>typedef void (CALLBACK * MMTIMERCALLBACK) (TRIGGER eTrigger, UINT uID,
>int iVal);
>
>CMMTimer


class CMMTimer?

>
>{
>
>public:
>
> BOOL Stop();
>
> BOOL Start(UINT uPeriod);
>
> BOOL Initialize(UINT uResolution);
>
> CMMTimer();
>
> virtual ~CMMTimer();
>
> void SetTimerCallbackFunction(MMTIMERCALLBACK pCallback);
>
>
>
>private:
>
> MMTIMERCALLBACK m_pCallback;
>
> UINT m_uResolution;
>
> UINT m_uPeriod;
>
> UINT m_uTimerID;
>
> BOOL m_bInitialized;
>
> static CMMTimer *pThis;


I don't see any use of pThis. Remove it.

>
> static void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser,
> DWORD dw1, DWORD dw2);
>
>}
>CMMTimer* CMMTimer:This = NULL;


Again, scrub this.

>void CMMTimer::SetTimerCallbackFunction(MMTIMERCALLBACK pCallback)
>
>{
>
> m_pCallback = pCallback;
>
>}
>
>
>
>BOOL CMMTimer::Start(UINT uPeriod)
>{
> if (!m_bInitialized) return FALSE;
> m_uPeriod = uPeriod;
> pThis = this;


What if you have more than one timer at a time?

In any case, you need to check the callback here:

if (!m_pCallback)
{
return FALSE;
}

and make sure m_pCallback is initialized to NULL by your constructor.

> m_uTimerID =
> timeSetEvent(
> m_uPeriod,
> m_uResolution,
> TimerProc,
> (DWORD) this,
> TIME_PERIODIC );
> if(! m_uTimerID)
> return FALSE;
> else
> return TRUE;
>}


That looks ok.

>void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD
>dw1, DWORD dw2)
>
>{
>
>// I tried this
>
> CMMTimer * pseudoThis = (CMMTimer *) dwUser;
>
> pseudoThis->m_pCallback(<TIMERT, 0, 0); #error#


That looks correct, apart from "<TIMERT" which isn't valid C++.

>// and this
>
> pThis->m_pCallback(<TIMERT, 0, 0); #error#


No need for this. Where is pThis initialized, anyway?

>}
>
>I also tried helper functions.


How so?

>The problem is that this code compiles perfectly, but when I execute the
>code, the debugger shows problems at the lines #error#. m_pCallback
>cannot be called, the pointer points at memory address 0xcccccccccccccc


That suggests that m_pCallback hasn't been initialized. Perhaps you
are calling SetTimerCallbackFunction on a different instance of your
timer class to the one you call Start on.

>I really would like this problem solved.


The code you've posted looks generally ok. It must be the code using
your timer class that is at fault. However, drop the static member
variable, and add the test to Start, and you'll probably find your
problem.

Tom


tom_usenet
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
equivalent function for itoa in Linux gcc compiler suse Software 0 03-06-2009 05:30 AM
How to assign a returns value of a javascript function to a hiddenfield in a webpart Chander Software 0 12-20-2007 09:14 AM
Invalid postback or callback argument chaitrak Software 0 06-27-2007 10:01 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46