-
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?
-
July 22nd, 2010, 01:06 PM
#2
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.
-
July 23rd, 2010, 09:32 AM
#3
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.
-
July 23rd, 2010, 11:16 AM
#4
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.
-
July 27th, 2010, 03:23 PM
#5
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.
-
August 12th, 2010, 01:57 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|