Click to See Complete Forum and Search --> : ADDRESS ERROR IN THE VIRTUAL MEMORY
bozon128
March 1st, 2000, 04:30 AM
hello,
I develop an application with Visual C++ 6.0 under Windows NT 4.0.When I compile it in Debug mode, all is good but when I throw my EXE some error messages appears on the screen:
- The first message means that an instruction try to access to a forbidden zone of the virtual memory; this zone is in the high part of the virtual memory and the address "accessed" is: 0xcdcdcdcd.
- The second message is
"Unhanled exception in <my EXE>:0xC0000005:Access Violation"
So I read that 0xcdcdcdcd is an address where is the core of NT and it's a protected zone for everything.
I can't choose this address and I don't know how to resolve my problem. Does anyone have a solution?
Thanks in advance for your help
bozon128
nikb
March 20th, 2000, 10:11 AM
You are trying to access an uninitialized or deleted variable.
MFC uses that value for pointers that are uninitialized or deleted - I don't remember which of the two offhand.
Check your call stack when the program breaks. It should give useful insight on what operation caused the error...
kranthi
March 21st, 2000, 11:34 AM
I had faced similar problems before and i will try to highlight what i think could be the actual problem and the solution to it. Atleast it worked in my case.
If u are allocating memory using VirtualAlloc, or HeapAlloc or GlobalAllocPtr, please check the return type to be corect.
Such a problem occours when the memory allocation fails(which i dont think is the problem in ur case), or u try to access a memory location that is outside the allocated limit.
The other cause for this problem, which is a little tough to explain, is the freeing of the memory.
In my code, if i had not freed the allocated memory in the distructor, then the program used to terminate gracefully but if i deleted the pointer i used to get the same error.
Ensure that the pointer is terminated properly with the proper call. Like if u are newing it the delete it, GlobalAlloc-GlobalFree and like that.
It would even be better if u have a seperate function calledcleanup, where u can free all ur program memory allocations.
One of these would surely help.
regards
Kranthi Kumar Katta.
Stege
March 24th, 2000, 10:46 AM
Hi
This is not a bug, but a feature !
VC will, if in DEBUG mode, set all your pointers to point to that address (0xcdcdcdcd), because it can check this value as 'uninitialized pointer'.
However, VC will not do this in release mode...
Be sure to check your code for illegal pointer access, i.e.
- ALWAYS initialize a pointer to NULL before allocating memory to it
- ALWAYS check the returned pointer against NULL (no memory available)
- it is common to fill up newly created mem-buffers with 0-Values
- check a pointer against NULL, before accessing it's value
...
// some examples
char *pText = NULL; // always do this
pText = new char[128]; // or : pText = (char *)malloc(128);
if (pText == NULL) return; // if no mem available, just jump back to caller
memset(pText,0,128); // remove garbage from buffer
strcpy(pText,"now you can access the memory, but be sure not to exceed 128 bytes...");
delete pText; // or : free(pText);
// after all, if pointer is reused, set it to NULL again
pText = NULL;
...
btw : it is safe to delete a NULL-pointer, BUT it's not good to delete an invalid/uninitialized pointer.
This code just works fine...
char *pText = NULL;
delete pText; // no problem
This code will generate access-violation
char *pText; // uninitialized, could point anywhere
delete pText;
hope, that helps
Stege
codeguru.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.