Intel sponsors gamedev.net search:   
Promit's VentspaceBy Promit      

Welcome to Ventspace! Most posts here are delayed copies of posts from the real Ventspace.

Thursday, October 1, 2009
Let's look at a real, in the trenches SlimDX bug that results from a problematic subtlety in .NET. This one isn't even fixed in trunk yet. Here's our main function of interest:
generic<typename T> where T : value class
        Result BaseEffect::SetValue( EffectHandle^ parameter, T value )
        {
                HRESULT hr;
                D3DXHANDLE handle = parameter != nullptr ? parameter->InternalHandle : NULL;

                if( T::typeid == bool::typeid )
                {
                        BOOL newValue = Convert::ToInt32( value, CultureInfo::InvariantCulture );
                        hr = InternalPointer->SetBool( handle, newValue );
                }
                else if( T::typeid == float::typeid )
                {
                        hr = InternalPointer->SetFloat( handle, static_cast<FLOAT>( value ) );
                }
                else if( T::typeid == int::typeid )
                {
                        hr = InternalPointer->SetInt( handle, static_cast<INT>( value ) );
                }
                else if( T::typeid == Matrix::typeid )
                {
                        hr = InternalPointer->SetMatrix( handle, reinterpret_cast<D3DXMATRIX*>( &value ) );
                }
                else if( T::typeid == Vector4::typeid )
                {
                        hr = InternalPointer->SetVector( handle, reinterpret_cast<D3DXVECTOR4*>( &value ) );
                }
                else
                {
                        hr = InternalPointer->SetValue( handle, &value, static_cast<DWORD>( sizeof(T) ) );
                }

                return RECORD_D3D9( hr );
        }








This function will crash randomly on good input. Specifically, D3D will return a D3DERR_INVALIDCALL, which then gets translated to an exception. Again, that's on known-good input. The call to the function looks like this:
effect.SetValue<float>("alpha", 0.5f);

When this call is made, an EffectHandle is implicitly constructed that copies the string into native memory. Here's the relevant bits of EffectHandle:
        EffectHandle::EffectHandle( String^ name )
        {
                m_StringData = Marshal::StringToHGlobalAnsi( name );
                m_HasString = true;
                m_StringDataSize = name->Length;
                GC::AddMemoryPressure( m_StringDataSize );

                m_Handle = reinterpret_cast<D3DXHANDLE>( m_StringData.ToPointer() );
        }

//Called by both ~EffectHandle and !EffectHandle.
        void EffectHandle::Destruct()
        {
                if( m_HasString )
                {
                        Marshal::FreeHGlobal( m_StringData );
                        GC::RemoveMemoryPressure( m_StringDataSize );
                }
        }









So. Do you see where we screwed this up?

In case it's not clear:
* EffectHandle::InternalHandle returns EffectHandle::m_Handle
* D3DXHANDLE is a typedef for char*
* Although there's plenty of interop, the interop is NOT the root cause.

HINT: EffectHandle is finalizable. Answer is in comments.





















Comments: 12 - Leave a Comment

Link


All times are ET (US)

 
S
M
T
W
T
F
S
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

OPTIONS
Track this Journal

 RSS 

ARCHIVES
October, 2009
September, 2009
August, 2009
July, 2009
June, 2009
October, 2008
June, 2008
May, 2008
April, 2008
March, 2008
February, 2008
January, 2008
December, 2007
November, 2007
October, 2007
September, 2007
August, 2007
July, 2007
June, 2007
May, 2007
February, 2007