Fixed exception at shutdown if the controllers are closed after the HIDDeviceManager is shutdown
authorSam Lantinga <slouken@libsdl.org>
Fri, 13 Mar 2020 13:05:32 -0700
changeset 13614a2bfecfe4ec2
parent 13613 65d1b7b419a2
child 13615 04a241d30c91
Fixed exception at shutdown if the controllers are closed after the HIDDeviceManager is shutdown
src/hidapi/android/hid.cpp
     1.1 --- a/src/hidapi/android/hid.cpp	Fri Mar 13 09:43:48 2020 -0700
     1.2 +++ b/src/hidapi/android/hid.cpp	Fri Mar 13 13:05:32 2020 -0700
     1.3 @@ -436,6 +436,12 @@
     1.4  		g_JVM->AttachCurrentThread( &env, NULL );
     1.5  		pthread_setspecific( g_ThreadKey, (void*)env );
     1.6  
     1.7 +		if ( !g_HIDDeviceManagerCallbackHandler )
     1.8 +		{
     1.9 +			LOGV( "Device open without callback handler" );
    1.10 +			return false;
    1.11 +		}
    1.12 +
    1.13  		m_bIsWaitingForOpen = false;
    1.14  		m_bOpenResult = env->CallBooleanMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerOpen, m_nId );
    1.15  		ExceptionCheck( env, "BOpen" );
    1.16 @@ -545,11 +551,18 @@
    1.17  		g_JVM->AttachCurrentThread( &env, NULL );
    1.18  		pthread_setspecific( g_ThreadKey, (void*)env );
    1.19  
    1.20 -		jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
    1.21 -		int nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendOutputReport, m_nId, pBuf );
    1.22 -		ExceptionCheck( env, "SendOutputReport" );
    1.23 -
    1.24 -		env->DeleteLocalRef( pBuf );
    1.25 +		int nRet = -1;
    1.26 +		if ( g_HIDDeviceManagerCallbackHandler )
    1.27 +		{
    1.28 +			jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
    1.29 +			nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendOutputReport, m_nId, pBuf );
    1.30 +			ExceptionCheck( env, "SendOutputReport" );
    1.31 +			env->DeleteLocalRef( pBuf );
    1.32 +		}
    1.33 +		else
    1.34 +		{
    1.35 +			LOGV( "SendOutputReport without callback handler" );
    1.36 +		}
    1.37  		return nRet;
    1.38  	}
    1.39  
    1.40 @@ -560,10 +573,18 @@
    1.41  		g_JVM->AttachCurrentThread( &env, NULL );
    1.42  		pthread_setspecific( g_ThreadKey, (void*)env );
    1.43  
    1.44 -		jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
    1.45 -		int nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendFeatureReport, m_nId, pBuf );
    1.46 -		ExceptionCheck( env, "SendFeatureReport" );
    1.47 -		env->DeleteLocalRef( pBuf );
    1.48 +		int nRet = -1;
    1.49 +		if ( g_HIDDeviceManagerCallbackHandler )
    1.50 +		{
    1.51 +			jbyteArray pBuf = NewByteArray( env, pData, nDataLen );
    1.52 +			nRet = env->CallIntMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerSendFeatureReport, m_nId, pBuf );
    1.53 +			ExceptionCheck( env, "SendFeatureReport" );
    1.54 +			env->DeleteLocalRef( pBuf );
    1.55 +		}
    1.56 +		else
    1.57 +		{
    1.58 +			LOGV( "SendFeatureReport without callback handler" );
    1.59 +		}
    1.60  		return nRet;
    1.61  	}
    1.62  
    1.63 @@ -587,6 +608,12 @@
    1.64  		g_JVM->AttachCurrentThread( &env, NULL );
    1.65  		pthread_setspecific( g_ThreadKey, (void*)env );
    1.66  
    1.67 +		if ( !g_HIDDeviceManagerCallbackHandler )
    1.68 +		{
    1.69 +			LOGV( "GetFeatureReport without callback handler" );
    1.70 +			return -1;
    1.71 +		}
    1.72 +
    1.73  		{
    1.74  			hid_mutex_guard cvl( &m_cvLock );
    1.75  			if ( m_bIsWaitingForFeatureReport )
    1.76 @@ -657,8 +684,11 @@
    1.77  		g_JVM->AttachCurrentThread( &env, NULL );
    1.78  		pthread_setspecific( g_ThreadKey, (void*)env );
    1.79  
    1.80 -		env->CallVoidMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerClose, m_nId );
    1.81 -		ExceptionCheck( env, "Close" );
    1.82 +		if ( g_HIDDeviceManagerCallbackHandler )
    1.83 +		{
    1.84 +			env->CallVoidMethod( g_HIDDeviceManagerCallbackHandler, g_midHIDDeviceManagerClose, m_nId );
    1.85 +			ExceptionCheck( env, "Close" );
    1.86 +		}
    1.87  	
    1.88  		hid_mutex_guard dataLock( &m_dataLock );
    1.89  		m_vecData.clear();