I seem to be getting a really weird error. Basically, I have a class A that is meant to manage a disk cache for pixel data. From the main program, I create an object of A using:
A* obj = new A(...);
Then, I call a method to read a pixel from the disk:
Pixel pix = obj->read(...);
However, when I try to use the “this” pointer to access private member variables of A from within the read() method, I get an access violation error because the “this” pointer is uninitialized (set to 0xCCCCCCCC by MSVC 2012). However, I checked the value of the “obj” pointer returned by the constructor, and it seems to be a valid address.
My guess is that somehow the constructor failed, but why, then, would it have returned a pointer to the object? Alternatively, if the constructor didn’t fail, why is the “this” pointer uninitialized from within the class?
In Visual Studio C++, what are the memory allocation representations?:
At the moment you do
obj->, yourobjis not initialized. The two lines of code in the question are not your real code, or there is something important taking place in between.Plain stepping through using debugger will give you an answer to the question.
One of the possible causes is that you have 2+
objlocal variables in your function.