|
-
July 22nd, 2010, 08:37 AM
#1
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|