Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access violation writing location 0xcccccccc

For the past 2 days I've been stuck on a violation which I can't seem to get to go away. I've used break points and located where the error is, but I'm just hoping one of you will know what the issue is without me having to copy+paste all my code -.-

I'm getting

First-chance exception at 0x1027cb1a (msvcr100d.dll) in Escape.exe: 0xC0000005: Access violation writing location 0xcccccccc. Unhandled exception at 0x1027cb1a (msvcr100d.dll) in Escape.exe: 0xC0000005: Access violation writing location 0xcccccccc.

Now, a quick google search makes me think there's something peculiar going on. All the search results talk about pointers not actually pointing anywhere (0xccccccccc is a low memory address?).

I'm yet to use pointers in my code but either way I'll paste the function and point out the line the exception gets thrown (in bold):

void mMap::fillMap(){
    for(int i = 0; i <= 9; i++){
        for(int z = 0; z <= 19; z++){
            Tile t1;    // default Tile Type = "NULLTILE"
            myMap[i][z] = t1;
        }
    }
}

Now myMap is a 2d array of type Tile. I had this working a couple of days ago until I added some other classes and it all stopped working!

like image 608
Linky Avatar asked May 22 '11 03:05

Linky


2 Answers

Either an uninitialized pointer, or a pointer stored in memory that's been freed. I think cccccccc is the first and cdcdcdcd is the second, but it varies with compiler/library implementation.

For your particular code, probably myMap hasn't been allocated yet, then myMap[0][0] would result in an access attempt to 0xcccccccc.


It can also happen that myMap is the beginning of your class, and the class pointer was uninitialized:

class mMap
{
     Tile myMap[10][20];
public:
     void f() { myMap[0][0] = 0; }
};

mMap* what;
what->f(); // what is an invalid pointer

This happens because the member function is not virtual, so the compiler knows what code to run and passes the object pointer as a hidden parameter. Eventually the compiler emits a calculation like:

this + offsetof(Whatever::myMap) + z * sizeof(myMap[0]) + i * sizeof(myMap[0][0])

this, being uninitialized, is 0xcccccccc. Evidently the offsetof part is zero, and i and z are both zero the first time through your loop, so you get 0xcccccccc + 0 + 0 + 0 as the memory address.


To debug this, use the call stack and find the function that called fillMap. Then check in that function where the pointers used for member access (->) came from.

like image 71
Ben Voigt Avatar answered Oct 21 '22 05:10

Ben Voigt


On MSVC++ and in debug mode, the debugging memory allocator sets all returned memory to 0xcccccccc, as a way to find cases of undefined behavior. In all likelihood, you never initialized myMap , or some of the pointers inside of myMap. Check your initialization code for bugs.

like image 4
Billy ONeal Avatar answered Oct 21 '22 03:10

Billy ONeal