Link to home
Start Free TrialLog in
Avatar of gammae
gammae

asked on

Convert Multi Byte 2 Wide Char crash sometimes

I have a function that sometime cause my program to crash, I did not writethe function myself, could someone tell me what could be causing the crash?

Here's the function:

std::wstring multi_byte_2_wide_char(
            const char* pStr,
            int nLen,
            UINT nCodePage )
      {
            assert( pStr != NULL );
            assert( nLen >= 0 || nLen == -1 );

            // figure out how many wide characters we are going to get
            int nChars = MultiByteToWideChar( nCodePage , 0 , pStr , nLen , NULL , 0 ) ;
            if ( nLen == -1 ) --nChars ;
            if ( nChars == 0 ) return L"";

            // convert the narrow string to a wide string
            // nb: slightly naughty to write directly into the string like this
            std::wstring buf;
            buf.resize( nChars );
            MultiByteToWideChar( nCodePage , 0 , pStr , nLen ,
                  const_cast<wchar_t*>(buf.c_str()) , nChars ) ;

            return buf;
      }

Whe debugging, it seems to crash on the "return buf", here's the last function in the call stack is a malloc and it's where it crash.

Any idea someone?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gammae
gammae

ASKER

Thanks Jkr for your suggestion.

I tried to implement what you suggested, but now it crash doing the delete
Here's teh callstack:

wcslen(const wchar_t * wcs=0x00d20f08)  Line 44      C
ATL::ChTraitsCRT<wchar_t>::GetBaseTypeLength(const wchar_t * pszSrc=0x00d20f08)  Line 827 + 0x9 bytes      C++
std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::assign(const wchar_t * _Ptr=0x00d20f08)  Line 1070 + 0x9 bytes      C++
std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::operator=(const wchar_t * _Ptr=0x00d20f08)  Line 921      C++
litex::multi_byte_2_wide_char(const char * pStr=0x014cf614, int nLen=-1, unsigned int nCodePage=0)

When debuggin, it breaks in wcslen.c on this line:
        while( *eos++ ) ;

 
Hm, works fine here:

#include <windows.h>
#include <assert.h>

#include <string>

using namespace std;

std::wstring multi_byte_2_wide_char(
            const char* pStr,
            int nLen,
            UINT nCodePage )
      {
            assert( pStr != NULL );
            assert( nLen >= 0 || nLen == -1 );

            // figure out how many wide characters we are going to get
            int nChars = MultiByteToWideChar( nCodePage , 0 , pStr , nLen , NULL , 0 ) ;
            if ( nLen == -1 ) --nChars ;
            if ( nChars == 0 ) return L"";

            // convert the narrow string to a wide string
            // nb: slightly naughty to write directly into the string like this
            std::wstring buf;
            wchar_t* pTmp = new wchar_t[nChars + 1];
            MultiByteToWideChar( nCodePage , 0 , pStr , nLen ,
                  pTmp , nChars ) ;
            buf = pTmp;
            delete[] pTmp;

            return buf;
      }

void main () {

 wstring test = multi_byte_2_wide_char("test", strlen("test"), CP_ACP);

}

Is it possible that the string passed in is not null terminated?

If that case,

            wchar_t* pTmp = new wchar_t[nChars + 1];
            MultiByteToWideChar( nCodePage , 0 , pStr , nLen ,
                  pTmp , nChars ) ;
            std::wstring buf(pTmp,nChars);
            delete[] pTmp;

should help.
Avatar of gammae

ASKER


Thank you so much, it works now!
You're most welcome ;o)

But, you should close your questions when the problem is solved.