Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access violation writing location 0xCCCCCCCC

So i have one of the wierdest bug i've seen in my life. I've bought a DirectX 11 book which comes with some [sample code]:http://www.d3dcoder.net/d3d11.htm I am pretty sure at some point in time i managed to compile and run every single sample app, but now, i have a "Access violation writing location 0xCCCCCCCC" error at runtime.

Now, this is happening one the following line :

ShadowsApp::ShadowsApp(HINSTANCE hInstance) : D3DApp(hInstance)
{
    mMainWndCaption = L"Shadows Demo"; <- Crashes here !!!  
    mLastMousePos.x = 0;
    mLastMousePos.y = 0;
    ...
}

mMainWndCaption being declared like this in the .h

std::wstring mMainWndCaption;

and set with a default value in the constructor of the class ShadowsApp inherits from

D3DApp::D3DApp(HINSTANCE hInstance) : 
mhAppInst(hInstance),
mMainWndCaption(L"D3D11 Application"),...

I think, this is already quite odd ... Now the strangest part comes when i declare ANY variable of ANY type in the d3dApp.h, I no longer have the "Access violation writing location 0xCCCCCCCC" error, everything builds and run perfectly. As a C# programmer, this makes absolutely no sense to me. How can the declaration of a random variable in a class can "fix" such a thing ?!

Any suggestion would be greatly appreciated :-)

like image 967
Yann Avatar asked Jul 06 '13 02:07

Yann


2 Answers

This page has a good description and background of the various "magic values" you might encounter when dealing with stack and heap.

From the page:

If you are seeing the 0xcccccccc bit pattern it means that you are reading memory that is on the current thread stack that has not been initialised.

Given the code snippet you've posted so far, and what you've described about "fixing" it with another variable declared in the base class, it sounds like the base and derived objects might not be in agreement as to their memory layout. Are they in the same library or executable? Check your compilation flags and make sure they match.

One strategy is to reduce your problem down to the minimal set of steps to reproduce the problem. You can make a copy of your project and start removing fields and methods until it works, and see if that helps you isolate it further.

like image 124
holtavolt Avatar answered Sep 29 '22 16:09

holtavolt


"Access violation writing location 0xCCCCCCCC" error at runtime.

You're trying to use unitialized pointer under msvc in debug build.

Initialize pointer.

mMainWndCaption = L"Shadows Demo"; <- Crashes here !!!

Install breakpoint at this location, run application under debugger, and investigate contents of variables (within "watch" window, or by hovering mouse over individual variables), including this pointers.

like image 26
SigTerm Avatar answered Sep 29 '22 15:09

SigTerm