Game Development Community

Small bug in DXDiagNVUtil::lpcwstrToString

by Philipp Tull · in Torque Game Engine Advanced · 05/04/2006 (2:27 pm) · 3 replies

After upgrading from VC2003 to VS2005 Express I had TSE asserting on startup everytime. After some digging it turned out that DXDiagNVUtil::GetDisplayDeviceManufacturer returns the empty string on my rig (dxdiag shows "n/a" under "Device Manufacturer"); this in turn causes lpcwstrToString to return an uninitialized string.
Changing

mbBuf = new char[sz];
wcstombs( mbBuf, in_lpcwstr, sz ); // convert the string
to
mbBuf = new char[sz+1];
wcstombs( mbBuf, in_lpcwstr, sz+1 ); // convert the string

fixes the problem for me. Still doesn't explain why the string is empty in the first place or why it worked with the older Visual Studio... :/

About the author

Recent Threads

  • Mingw compiling

  • #1
    07/10/2006 (9:51 am)
    Hmm... Interesting catch. It was probably uninitialized memory issues that made it work on one and not the other. Does that cause it to read more uninitialized memory, though?
    #2
    07/10/2006 (4:42 pm)
    Ya, looks like you have the right idea, but it's still not quite safe. I've changed it to

    string DXDiagNVUtil::lpcwstrToString( const LPCWSTR in_lpcwstr )
    {
       //@ consider using windows.h  WideCharToMultiByte(..)
       char * mbBuf;
       size_t sz;
    
       if( !in_lpcwstr || !in_lpcwstr[0] )
       {
          mbBuf = new char[2];
          mbBuf[0] = mbBuf[1] = 0;
       }
       else
       {
          sz = 2 * wcslen( in_lpcwstr );
          mbBuf = new char[sz];
          wcstombs( mbBuf, in_lpcwstr, sz );		// convert the string 
       }
       string outstr;
       outstr = mbBuf;
       SAFE_ARRAY_DELETE( mbBuf );
       return( outstr );
    }

    See if that works for ya.
    #3
    07/10/2006 (4:53 pm)
    Nice fix, Brian. :)