none
Access violation reading location 0x0000000000000000

    Question

  • I am trying to learn programming using directx and while trying the following code, I get the error:

    Unhandled exception at 0x000007f7d84d17b5 in TriangleDemo.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

    code:

    ID3DBlob* vsBuffer=0;

        bool compileResult = CompileD3DShader("SolidGreenColor.fx","VS_Main","vs_5_0",&vsBuffer);

        HRESULT d3dResult;
        d3dResult= d3dDevice_->CreateVertexShader(vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), 0, &solidColorVS_);

        if(FAILED(d3dResult))
        {
            if(vsBuffer)
                vsBuffer->Release();

            return false;
        }

    the doesn't seem to find the file SolidGreenColor.fx.

    How do I fix this?

    Tuesday, September 03, 2013 5:31 PM

Answers

  • Access violation reading location 0 means that you tried to read a null pointer.

    Wherever you have something of the form:  foo->bar(), you should check to see if foo is null first.

    So glancing at your code, the two candidates for null pointers are:

    • d3dDevice_ in the call to d3dDevice_->CreateVertexShader  [unlikely.]
    • vsBuffer in the call to vsBuffer->GetBufferPointer() or vsBuffer->GetBufferSize()  [very likely!]

    My guess is that your shader failed to compile, and vsBuffer is null.  Check the compileResult to see if your shader compiled before calling CreateVertexShader.  Also, delve into the CompileD3DShader function to see what happened.

    I'm assuming you are using this implementation of the CompileD3DShader function from the TriangleDemo sample application.

    boolDx11DemoBase::CompileD3DShader(char*filePath,char*entry,char*
    								   shaderModel,ID3DBlob**buffer)
    {
    	DWORDshaderFlags=D3DCOMPILE_ENABLE_STRICTNESS;
    #ifdefined(DEBUG) ||defined(_DEBUG)
    	shaderFlags|=D3DCOMPILE_DEBUG;
    #endif
    	ID3DBlob*errorBuffer=0;
    	HRESULTresult;
    	result=D3DX11CompileFromFile(filePath,0,0,entry,shaderModel,
    		shaderFlags,0,0,buffer,&errorBuffer,0);
    	if(FAILED(result))
    	{
    		if(errorBuffer!=0)
    		{
    			OutputDebugStringA((char*)errorBuffer->GetBufferPointer());
    			errorBuffer->Release();
    		}
    		returnfalse;
    	}
    	if(errorBuffer!=0)
    		errorBuffer->Release();
    	return true;
    }

    ...in which case, you can further check the result of D3DX11CompileFromFile to see why it failed.  And if it's an actual problem with the code, then you can see the errorBuffer->GetBufferPointer() string to get an error message for the compile failure.  This implementation would have output the error to a debug string in the output window.

    Other reasons for the failure include a non-existant or inaccessible SolidGreenColor.fx file, or having the wrong path to the file (incorrect working directory, for example.)  The working directory is typically $(ProjectDir) for a default project.  Although I find it much more natural to have it be $(TargetDir).  To simulate more closely what would happen in an install scenario.  Just make sure the file is in a reasonable place where it can be opened, or specify the full path to the file.

    Wednesday, September 04, 2013 3:55 PM