Memory Corruption on Heap
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Memory Corruption on Heap

  1. #1
    Join Date
    May 2003
    Sao Paulo - Brazil

    Unhappy Memory Corruption on Heap


    I have some serious problem with memory corruption on the heap, using the new and delete [] operators with TCHAR * and MultiByteToWideChar() function.

    Iīve been working on an application for WinCE with VC++ Embeded and all the strings have to be converted to UNICODE. Up to here thatīs OK. The problem starts when I use a code like this :

       int iBufferLen;
       TCHAR * Buffer;
       Buffer = new TCHAR[ iBufferLen ] ;
       delete [] Buffer; // <- HERE HEAPENS THE BUG!
    And the BUG is :
    "When the program line execute the delete [] Buffer; another part of memory is erased and this causes memory corruption."

    Does anyone have any idea of what is going on ?

    Thanks in advanced.
    Best Regards.

    Marcelo Franco

  2. #2
    Join Date
    Jan 2007

    Re: Memory Corruption on Heap

    I know this was posted 9 years ago, but i couldnt help seeing a question that had no response

    Theres not enough information provided to answer the question properly, if you want a proper answer you must include ALL the code so we can see what is happening, it is foolish to assume that all the other code is right, and we aren't psychic.

    If there really is only a call to WideCharToMultiByte and the params are correct, all you need to do to see the problem is go to MSDN's page about WideCharToMultiByte. It is obvious that it is a dangerous call if misused and prone to give you errors. Most new programmers make the mistake of thinking that converting Wide chars (WCHAR) to multibyte is the same as converting it to 8-bit ascii. IT ISNT. The second mis-assumption is that the string will always be shorter. Depending on the content (and the params used calling the proc), you could end up with a string bigger than the one you started with, MBSTRs use the high bits to flag that the char is not a simple one and expands into 1 or more additional bytes to describe it. I don't know the specifics because I dont work with MBSTRs but I do know that they are not strictly all 1-byte-per-character. Sure, most strings would be, thats the deception.. and the first time you have to process a string with multi-byte chars, your dead.... I believe there is a proc or something that tells you how long the resulting string would be maybe even the same one your using, like I said, I dont work with em... just had to say this so others who come here via searches know that....
    remember: old threads are constantly read upon and the information they contain are widely used even if they are wrong, it is our job to put things right -- even if it takes 9 years or more ----

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)