CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2008
    Posts
    10

    Unmanaged item in managed class

    Environment: Visual Studio 2005
    Type of program: C++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?

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Unmanaged item in managed class

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

    This code is much more safe than yours. First, if object is disposed, finalizer is not called. Finalizer is called only if client doesn't dispose class instance (not all clients are smart enough to call Dispose method). Finalizer is never called twice. Anyway, finalizer sets pStr to NULL after releasing, it is always good to do this to prevent releasing the same pointer twice.

  3. #3
    Join Date
    Jul 2008
    Posts
    10

    Re: Unmanaged item in managed class

    Dear Alex F,

    I regret to inform you that your suggested changes do not solve the issue.
    In Debug mode, I still have a flashing error message and the _CrtCheckMemory asserts.

  4. #4
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Unmanaged item in managed class

    Set breakpoints in both functions. Run the program under debugger and see how these functions are called. When assertion message is shown, check in debugger, what is its reason.
    If you still have problems, post here additional information from the program debugging.
    Last edited by Alex F; July 23rd, 2010 at 12:24 PM.

  5. #5
    Join Date
    Jul 2008
    Posts
    10

    Re: Unmanaged item in managed class

    I apologize for the late reply. I code just for fun when I have time, and unfortunately I seldom have time.

    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.

  6. #6
    Join Date
    Aug 2010
    Posts
    51

    Smile Re: Unmanaged item in managed class

    hi,

    Run the program under debugger and see how these functions are called. When assertion message is shown, check in debugger, what is its reason.



    regards,
    phe9oxis,
    http://www.guidebuddha.com

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured