none
UNMANAGED ITEM IN MANAGED CLASS RRS feed

  • Question

  • Environment: Visual Studio 2005
    Type of program: CLR

    1. I have a class:

    public ref class Token
    {
     Byte* pStr;
    ...
    }

    Normally pStr is initialized as NULL; when required, it is initialized as:
    pStr = new Byte[len];

    It seems to run perfectly, but there is a hidden issue as I'll say below.

    2. Since pStr is unmanaged, I thought it a good idea to release the memory in the destructor / finalizer:

    Token::~Token() { this->!Token(); }
    Token::!Token() { if (pStr) delete [] pStr; }

    This flashes an error message when the program exits, stating HEAP CORRUPTION DETECTED.

    3. I added the following just before exiting my program:

    ...
    _ASSERTE(_CrtCheckMemory());
    return nRetCode;
    }

    It indeed asserts - regardless of wheteher I use the destructor / finalizer or not.

    My program runs perfectly with no reported  error if I simply remove destructor, finalizer and _ASSSERTE ... however, I am puzzled by this behaviour.

    Any explanation?

     

    Tuesday, July 20, 2010 4:29 PM

Answers

  • After reviewing my code ... I must admit that I fell in a very trivial error: allocation of pStr must be:

    pStr = new Byte[len+1];

    to take into account the 0-terminator.
    As you can see in my original code, I had forgotten that '+1'. Now it runs fine.
    Thank you for your help!

     

    • Marked as answer by GiorgioIT Tuesday, July 27, 2010 8:28 PM
    Tuesday, July 27, 2010 8:27 PM
  • Hi,

      Just based on your code snippets, it seems fine. Could you provide some more code snippets about how you use the p->Str? And the detailed error messages when the error happened?  It's probably related to the out-of-range use of the memory allocated to the pointer. For example, if the memory area pointed by the pStr is not enough to hold the data, there could be heap corruption errors.

      Below is a similar thread:

      http://social.msdn.microsoft.com/forums/en-US/asmxandxml/thread/71d58e4c-e054-4bbe-8371-cb9cdc1d2578

      BTW: please consider the Visual C++ forums as a better option.

     


    Please mark the right answer at right time.
    Thanks,
    Sam

     

    • Edited by SamAgain Monday, July 26, 2010 8:57 AM refine
    • Marked as answer by SamAgain Tuesday, July 27, 2010 10:16 AM
    Monday, July 26, 2010 8:56 AM
  • We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by GiorgioIT Tuesday, July 27, 2010 8:28 PM
    Tuesday, July 27, 2010 10:16 AM

All replies

  • Hi,

      Just based on your code snippets, it seems fine. Could you provide some more code snippets about how you use the p->Str? And the detailed error messages when the error happened?  It's probably related to the out-of-range use of the memory allocated to the pointer. For example, if the memory area pointed by the pStr is not enough to hold the data, there could be heap corruption errors.

      Below is a similar thread:

      http://social.msdn.microsoft.com/forums/en-US/asmxandxml/thread/71d58e4c-e054-4bbe-8371-cb9cdc1d2578

      BTW: please consider the Visual C++ forums as a better option.

     


    Please mark the right answer at right time.
    Thanks,
    Sam

     

    • Edited by SamAgain Monday, July 26, 2010 8:57 AM refine
    • Marked as answer by SamAgain Tuesday, July 27, 2010 10:16 AM
    Monday, July 26, 2010 8:56 AM
  • We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by GiorgioIT Tuesday, July 27, 2010 8:28 PM
    Tuesday, July 27, 2010 10:16 AM
  • After reviewing my code ... I must admit that I fell in a very trivial error: allocation of pStr must be:

    pStr = new Byte[len+1];

    to take into account the 0-terminator.
    As you can see in my original code, I had forgotten that '+1'. Now it runs fine.
    Thank you for your help!

     

    • Marked as answer by GiorgioIT Tuesday, July 27, 2010 8:28 PM
    Tuesday, July 27, 2010 8:27 PM
  • Hi,

     Thanks for sharing your result. Glad the problem is solved.


    Please mark the right answer at right time.
    Thanks,
    Sam
    Tuesday, August 3, 2010 12:55 PM