Sign in to follow this  

DirectInput, debug info shows leak

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i've recently purchased a XBox 360 controller for PC since i wanted to dabble with force feedback. Since the standard driver does NOT provide force feedback i installed the XBCD driver which works fine. I'm using XP Pro with SP2 installed. I've used the FFConst sample as a base from the October 2006 SDK which works nicely. To make sure i've got clean code i turned on debug DirectX in the control panel (debug to the max). Now everything works fine but it shows me this line: DINPUT8: unloaded before all objects released. (cRef:3) I've double checked and i am releasing everything that needs to be released. This same message also shows up with the original sample code. I've narrowed the offending code down by commenting lines, the problem seems to be the call: if( FAILED( hr = g_pDevice->SetProperty( DIPROP_AUTOCENTER, &dipdw.diph ) ) ) return hr; Now SetProperty shouldn't do anything in regard to reference counting AFAIK. There also is no UnSetProperty call (wouldn't make sense anyway). If i initialise, release completely and repeat this, the leak count goes up to 6 (it does add up). Anyone have any idea? Below is the full code i'm using, put into a simple MFC app. There would be more code after SetProperty but the error also pops up with this piece. Any obvious mistakes in there?
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#pragma comment( lib, "dxguid.lib" )
#pragma comment( lib, "dinput8.lib" )


LPDIRECTINPUT8        g_pDI       = NULL;         
LPDIRECTINPUTDEVICE8  g_pDevice   = NULL;
LPDIRECTINPUTEFFECT   g_pEffect   = NULL;
BOOL                  g_bActive   = TRUE;
DWORD                 g_dwNumForceFeedbackAxis = 0;

HRESULT InitDirectInput( HWND hWnd )
{
    DIPROPDWORD dipdw;
    HRESULT     hr;

    // Register with the DirectInput subsystem and get a pointer
    // to a IDirectInput interface we can use.
    if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, 
                                         IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) )
    {
        return hr;
    }
    
    // Look for a force feedback device we can use
    if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, 
                                         EnumFFDevicesCallback, NULL, 
                                         DIEDFL_ATTACHEDONLY | DIEDFL_FORCEFEEDBACK ) ) )
    {
        return hr;
    }

    if( NULL == g_pDevice )
    {
        MessageBox( NULL, _T("Force feedback device not found. ")
                          _T("The sample will now exit."), 
                          _T("FFConst"), MB_ICONERROR | MB_OK );
        return S_OK;
    }

    // Set the data format to "simple joystick" - a predefined data format. A
    // data format specifies which controls on a device we are interested in,
    // and how they should be reported.
    //
    // This tells DirectInput that we will be passing a DIJOYSTATE structure to
    // IDirectInputDevice8::GetDeviceState(). Even though we won't actually do
    // it in this sample. But setting the data format is important so that the
    // DIJOFS_* values work properly.
    if( FAILED( hr = g_pDevice->SetDataFormat( &c_dfDIJoystick ) ) )
        return hr;

    // Set the cooperative level to let DInput know how this device should
    // interact with the system and with other DInput applications.
    // Exclusive access is required in order to perform force feedback.
    if( FAILED( hr = g_pDevice->SetCooperativeLevel( hWnd,
                                                     DISCL_EXCLUSIVE | 
                                                     DISCL_FOREGROUND ) ) )
    {
        return hr;
    }

    // Since we will be playing force feedback effects, we should disable the
    // auto-centering spring.
    dipdw.diph.dwSize       = sizeof(DIPROPDWORD);
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    dipdw.diph.dwObj        = 0;
    dipdw.diph.dwHow        = DIPH_DEVICE;
    dipdw.dwData            = FALSE;

    if( FAILED( hr = g_pDevice->SetProperty( DIPROP_AUTOCENTER, &dipdw.diph ) ) )
        return hr;

    return S_OK;
}



VOID FreeDirectInput()
{
    // Unacquire the device one last time just in case 
    // the app tried to exit while the device is still acquired.
    // Release any DirectInput objects.
    if ( g_pEffect )
    {
      g_pEffect->Stop();
      g_pEffect->Unload();
      dh::Log( "Effect Refs %d", g_pEffect->Release() );
      g_pEffect = NULL;
    }
    if ( g_pDevice )
    {
      g_pDevice->Unacquire();
      dh::Log( "device Refs %d", g_pDevice->Release() );
      g_pDevice = NULL;
    }
    if ( g_pDI )
    {
      dh::Log( "DI Refs %d", g_pDI->Release() );
      g_pDI = NULL;
    }
}



Share this post


Link to post
Share on other sites

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this