-8

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?

user1871183
  • 439
  • 5
  • 11
  • Short: It can't. Long: show code. – PlasmaHH Jan 19 '13 at 22:44
  • 2
    Something in between the two lines of code you have shown has clobbered the `obj` variable (not necessarily the object it points to). The best tool I know for finding this kind of bug is [`valgrind`](http://valgrind.org/), but I don't know if it works on Windows. – zwol Jan 19 '13 at 22:45
  • Unfortunately, I can't show the code, as I don't have permission/rights to do so. – user1871183 Jan 19 '13 at 22:50
  • 1
    @user1871183 How do you expect us to debug it then? – melpomene Jan 19 '13 at 22:51
  • @melpomene I posted on the off chance that it was something common or that someone had a good idea. If all I'm going to get is attitude, forget about it. – user1871183 Jan 19 '13 at 22:53

1 Answers1

2

In Visual Studio C++, what are the memory allocation representations?:

  • 0xCCCCCCCC : Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory

At the moment you do obj->, your obj is 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+ obj local variables in your function.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • I just checked, and there is only one creation of the object in the calling function. Thanks for the help – user1871183 Jan 19 '13 at 22:55
  • it might be `A* obj = new A(...); /* other code */ { A* obj; /* other code */ obj->read(...)` It can be anything else but direct bug in code right there. – Roman R. Jan 19 '13 at 22:57
  • I don't think so. There's only one creation of the object in the entire application. Its meant to be a singleton of sorts. – user1871183 Jan 19 '13 at 22:58
  • Got it! After stepping all the way up to the main level, I realized that I initialized it in the initializer list for the calling object as well as in the code. Thanks for your help :) – user1871183 Jan 19 '13 at 23:04