troubleshooting Question

Convert Multi Byte 2 Wide Char crash sometimes

Avatar of gammae
gammae asked on
5 Comments1 Solution1251 ViewsLast Modified:
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?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros