Using function pointer in callback function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pvdm

    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
    SetTimerCallbac kFunction. 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 SetTimerCallbac kFunction(MMTIM ERCALLBACK 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::pThis = NULL;



    void CMMTimer::SetTi merCallbackFunc tion(MMTIMERCAL LBACK pCallback)

    {

    m_pCallback = pCallback;

    }



    BOOL CMMTimer::Start (UINT uPeriod)

    {

    if (!m_bInitialize d) 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(<T IMERT, 0, 0); #error#

    // and this

    pThis->m_pCallback(<T IMERT, 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 0xccccccccccccc c



    I really would like this problem solved.



    Thank you



    pvdm





    }


    --
    Posted via http://dbforums.com
  • tom_usenet

    #2
    Re: Using function pointer in callback function

    On Tue, 09 Sep 2003 05:02:58 -0400, pvdm <member38698@db forums.com>
    wrote:
    [color=blue]
    >
    >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.[/color]

    Why do you need pThis? You are passing the this argument to the
    callback anyway.
    [color=blue]
    >timeSetEvent uses TimerProc to set the callback function.[/color]
    [color=blue]
    >m_pCallback is a function that is passed using the
    >SetTimerCallba ckFunction. 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.[/color]

    Right, sounds reasonable.
    [color=blue]
    >Header file looks like this:
    >
    >typedef void (CALLBACK * MMTIMERCALLBACK ) (TRIGGER eTrigger, UINT uID,
    >int iVal);
    >
    >CMMTimer[/color]

    class CMMTimer?
    [color=blue]
    >
    >{
    >
    >public:
    >
    > BOOL Stop();
    >
    > BOOL Start(UINT uPeriod);
    >
    > BOOL Initialize(UINT uResolution);
    >
    > CMMTimer();
    >
    > virtual ~CMMTimer();
    >
    > void SetTimerCallbac kFunction(MMTIM ERCALLBACK pCallback);
    >
    >
    >
    >private:
    >
    > MMTIMERCALLBACK m_pCallback;
    >
    > UINT m_uResolution;
    >
    > UINT m_uPeriod;
    >
    > UINT m_uTimerID;
    >
    > BOOL m_bInitialized;
    >
    > static CMMTimer *pThis;[/color]

    I don't see any use of pThis. Remove it.
    [color=blue]
    >
    > static void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser,
    > DWORD dw1, DWORD dw2);
    >
    >}
    >CMMTimer* CMMTimer::pThis = NULL;[/color]

    Again, scrub this.
    [color=blue]
    >void CMMTimer::SetTi merCallbackFunc tion(MMTIMERCAL LBACK pCallback)
    >
    >{
    >
    > m_pCallback = pCallback;
    >
    >}
    >
    >
    >
    >BOOL CMMTimer::Start (UINT uPeriod)
    >{
    > if (!m_bInitialize d) return FALSE;
    > m_uPeriod = uPeriod;
    > pThis = this;[/color]

    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.
    [color=blue]
    > m_uTimerID =
    > timeSetEvent(
    > m_uPeriod,
    > m_uResolution,
    > TimerProc,
    > (DWORD) this,
    > TIME_PERIODIC );
    > if(! m_uTimerID)
    > return FALSE;
    > else
    > return TRUE;
    >}[/color]

    That looks ok.
    [color=blue]
    >void CALLBACK TimerProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD
    >dw1, DWORD dw2)
    >
    >{
    >
    >// I tried this
    >
    > CMMTimer * pseudoThis = (CMMTimer *) dwUser;
    >
    > pseudoThis->m_pCallback(<T IMERT, 0, 0); #error#[/color]

    That looks correct, apart from "<TIMERT" which isn't valid C++.
    [color=blue]
    >// and this
    >
    > pThis->m_pCallback(<T IMERT, 0, 0); #error#[/color]

    No need for this. Where is pThis initialized, anyway?
    [color=blue]
    >}
    >
    >I also tried helper functions.[/color]

    How so?
    [color=blue]
    >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 0xccccccccccccc c[/color]

    That suggests that m_pCallback hasn't been initialized. Perhaps you
    are calling SetTimerCallbac kFunction on a different instance of your
    timer class to the one you call Start on.
    [color=blue]
    >I really would like this problem solved.[/color]

    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

    Comment

    Working...