EDN Admin
Well-known member
Im learning c++ and I have been running into this problem when ever I try to delete dynamic memory. Here is the error that is returned when ever I am debugging a dynamic memory example.
Debug Assertion Failed!<br/>
<br/>
Program ...tudio2010Projectsmarch8thProjectDebugmarch8thProject.exe<br/>
File: f:ddvctoolscrt_bldself_x86crtsrcdbgdel.cpp<br/>
Line: 52<br/>
<br/>
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)<br/>
<br/>
For information on how your program can cause an assertion failure, see the visual c++ documentation on asserts.<br/>
<br/>
<br/>
<br/>
So once this message appears I am given three options Abort, Retry, Ignore. When I chose Ignore, it brings up the file
<br/>
"dbgheap.c" and points to this line of code
<br/>
<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; extern <span style="color:#A31515; "C" _CRTIMP <span style="color:Blue; int __cdecl _CrtIsValidHeapPointer(
<span style="color:Blue; const <span style="color:Blue; void * pUserData
)
{
<span style="color:Blue; if (!pUserData)
<span style="color:Blue; return FALSE;
<span style="color:Blue; if (!_CrtIsValidPointer(pHdr(pUserData), <span style="color:Blue; sizeof(_CrtMemBlockHeader), FALSE))
<span style="color:Blue; return FALSE;
<span style="color:Blue; return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}
[/code]
<br/>
When I look at the varialbes that are present in the auto tab I see
void* _crtheap== 0x00030000 and<br/>
const void *pUserData = 0x004199a0 string "2001 A Space Odyssesy".<br/>
<br/>
Once Im done with this, "output.c" is brought up in the work area, and Im seeing it is saying there are some
<br/>
bad pointers, for some of "output.c" variables. Does this mean that the pointers Im using in my code (which is
<br/>
included below) are not being properly used, or is this a visual c++ problem?<br/>
<br/>
HERE IS MY CODE<br/>
<div style="color:Black;background-color:White; <pre>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
<span style="color:Blue; using <span style="color:Blue; namespace std;
<span style="color:Blue; struct movies_t {
<span style="color:Blue; char *title;
<span style="color:Blue; int year;
<span style="color:Blue; float movieRating;
} mine, yours;
<span style="color:Blue; void printmovie (movies_t movie);
<span style="color:Blue; int main (){
string mystr;
mine.title = <span style="color:Blue; new (<span style="color:Blue; nothrow) <span style="color:Blue; char[];
mine.title = <span style="color:#A31515; "2001 A Space Odyssey";
mine.year = 1968;
mine.movieRating = 2.9;
cout<<<span style="color:#A31515; "Movie Title:"<< mine.title<<<span style="color:#A31515; "n";
cout<<<span style="color:#A31515; "Movie Year:"<<mine.year<<<span style="color:#A31515; "n";
cout<<<span style="color:#A31515; "Movie Rating:"<<mine.movieRating<<<span style="color:#A31515; "n";
<span style="color:Blue; delete [] mine.title;
cout<<<span style="color:#A31515; "mine.title after dynamic memory was deleted"<< mine.title<<<span style="color:#A31515; "n";
cout<<<span style="color:#A31515; "";
<span style="color:Blue; return 0;
}
[/code]
<br/>
Any Help with this issue is greatly appreciated<br/>
<br/>
Thank You
View the full article
Debug Assertion Failed!<br/>
<br/>
Program ...tudio2010Projectsmarch8thProjectDebugmarch8thProject.exe<br/>
File: f:ddvctoolscrt_bldself_x86crtsrcdbgdel.cpp<br/>
Line: 52<br/>
<br/>
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)<br/>
<br/>
For information on how your program can cause an assertion failure, see the visual c++ documentation on asserts.<br/>
<br/>
<br/>
<br/>
So once this message appears I am given three options Abort, Retry, Ignore. When I chose Ignore, it brings up the file
<br/>
"dbgheap.c" and points to this line of code
<br/>
<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; extern <span style="color:#A31515; "C" _CRTIMP <span style="color:Blue; int __cdecl _CrtIsValidHeapPointer(
<span style="color:Blue; const <span style="color:Blue; void * pUserData
)
{
<span style="color:Blue; if (!pUserData)
<span style="color:Blue; return FALSE;
<span style="color:Blue; if (!_CrtIsValidPointer(pHdr(pUserData), <span style="color:Blue; sizeof(_CrtMemBlockHeader), FALSE))
<span style="color:Blue; return FALSE;
<span style="color:Blue; return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}
[/code]
<br/>
When I look at the varialbes that are present in the auto tab I see
void* _crtheap== 0x00030000 and<br/>
const void *pUserData = 0x004199a0 string "2001 A Space Odyssesy".<br/>
<br/>
Once Im done with this, "output.c" is brought up in the work area, and Im seeing it is saying there are some
<br/>
bad pointers, for some of "output.c" variables. Does this mean that the pointers Im using in my code (which is
<br/>
included below) are not being properly used, or is this a visual c++ problem?<br/>
<br/>
HERE IS MY CODE<br/>
<div style="color:Black;background-color:White; <pre>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
<span style="color:Blue; using <span style="color:Blue; namespace std;
<span style="color:Blue; struct movies_t {
<span style="color:Blue; char *title;
<span style="color:Blue; int year;
<span style="color:Blue; float movieRating;
} mine, yours;
<span style="color:Blue; void printmovie (movies_t movie);
<span style="color:Blue; int main (){
string mystr;
mine.title = <span style="color:Blue; new (<span style="color:Blue; nothrow) <span style="color:Blue; char[];
mine.title = <span style="color:#A31515; "2001 A Space Odyssey";
mine.year = 1968;
mine.movieRating = 2.9;
cout<<<span style="color:#A31515; "Movie Title:"<< mine.title<<<span style="color:#A31515; "n";
cout<<<span style="color:#A31515; "Movie Year:"<<mine.year<<<span style="color:#A31515; "n";
cout<<<span style="color:#A31515; "Movie Rating:"<<mine.movieRating<<<span style="color:#A31515; "n";
<span style="color:Blue; delete [] mine.title;
cout<<<span style="color:#A31515; "mine.title after dynamic memory was deleted"<< mine.title<<<span style="color:#A31515; "n";
cout<<<span style="color:#A31515; "";
<span style="color:Blue; return 0;
}
[/code]
<br/>
Any Help with this issue is greatly appreciated<br/>
<br/>
Thank You
View the full article