Question

I've took a working CSprite class from my old DirectX 11.0 project which i builded with Visual Studio 2010. I just modified it a little bit, I extract some varibles, getter & setter and put it in a new class named CObject. Now i want to build with Visual Studio 2012.

But now I get everytime I try to create a object of CSprite a "Access Violation" error from Visual Studio:

Unhandled exception at 0x010A74A8 in Engine.exe: 0xC0000005: Access violation reading location 0x00000000.

It seems to be i can't create any DirectX 11 interface object and if I want to read out the object Visual Studio just say's "Unable to read memory".

Sourcecode:

Object.h

#ifndef __OBJECT_H__
#define __OBJECT_H__

#include "../DX11Core/DX11Util.h"

class CObject
{
public:
    CObject();
    ~CObject(){}

    void SetStatus( STRING _status )    { m_status = _status; }
    void SetTag( STRING _tag )          { m_tag = _tag; }
    void SetPosition( float _x, float _y, float _z ) { SetPosition( XMFLOAT3( _x, _y, _z ) ); }
    void SetPosition( XMFLOAT3 _pos )   { m_position = _pos; }
    void SetRotation( float _rot )      { m_fRotation = _rot; }
    void SetScale( float _x, float _y ) { SetScale( XMFLOAT3( _x, _y, 1 ) ); }
    void SetScale( XMFLOAT3 _scale )    { m_scale = _scale; }
    void SetAlpha( float _alpha )       { m_fAlpha = Clamp( _alpha, 0.0f, 1.0f ); }
    void SetActive( bool _active )      { m_bActive = _active; }

    XMFLOAT3&   GetPosition()           { return m_position; }
    STRING      GetStatus()             { return m_status; }
    STRING      GetTag()                { return m_tag; }
    float       GetXPosition()          { return m_position.x; }
    float       GetYPosition()          { return m_position.y; }
    float       GetZPosition()          { return m_position.z; }
    float       GetRotation()           { return m_fRotation; }
    XMFLOAT3&   GetScale()              { return m_scale; }
    float       GetXScale()             { return m_scale.x; }
    float       GetYScale()             { return m_scale.y; }
    bool        IsActive()              { return m_bActive; }

protected:
    XMFLOAT3    m_position;
    STRING      m_tag;
    STRING      m_status;
    float       m_fRotation;
    XMFLOAT3    m_scale;
    float       m_fAlpha;
    bool        m_bActive;
};

#endif // __OBJECT_H__

Sprite.h

#ifndef __SPRITE_H__
#define __SPRITE_H__

#include "Object.h"
#include "../DX11Core/DX11Core.h"
#include "../DX11Core/GraphicHelper.h"

struct Vertex
{
    XMFLOAT3 Pos;
    XMFLOAT2 Tex;
};

class CSprite : public CObject
{
public:
    CSprite( STRING _filename );
    ~CSprite()
    {
        RELEASE_COM( m_pVertices );
        RELEASE_COM( m_pTextureSampler );
        RELEASE_COM( m_pTextureSRV );
    }

    int Initialize();
    int Update( const float _dt );
    void Render();

#pragma region Getter & Setter

    void SetTexture( STRING _filename );
    void SetFrameCount( int _framecount ) { m_nFrameCount = _framecount; }
    void SetFrame( int _frameidx )
    {
        if( _frameidx >= 0 && _frameidx < m_nFrameCount )
        {
            m_nActualFrame = _frameidx;
        }
        BuildVertices();
    }
    void SetPosition( XMFLOAT3& _pos )
    {
        m_position = _pos;
        m_position.x -= ( theDXCore->GetClientWidth() / 2 );
        m_position.y *= -1.0f;
        m_position.y += ( theDXCore->GetClientHeight() / 2 );
    }

    float       GetLeftBounding() const { return ( m_position.x + m_fBoundingLeft ); }
    float       GetRightBounding() const    { return ( m_position.x + m_fBoundingRight ); }
    float       GetTopBounding() const  { return ( m_position.y + m_fBoundingTop ); }
    float       GetBottomBounding() const { return ( m_position.y + m_fBoundingBottom ); }
    int         GetFrameIdx() const { return m_nActualFrame; }
    XMMATRIX    GetWorldMatrix() const;
    Vertex      GetVertex( int _i) const { return tempVertex[_i]; }

#pragma endregion

private:
    void BuildVertices();

    int         m_nFrameCount;
    int         m_nActualFrame;
    float       m_fFrameDT;
    float       m_fActualFrameDT;

    float       m_fBoundingLeft;
    float       m_fBoundingRight;
    float       m_fBoundingTop;
    float       m_fBoundingBottom;

    Vertex tempVertex[6];

    ID3D11Buffer*                           m_pVertices;
    ID3D11ShaderResourceView*               m_pTextureSRV;
    ID3D11SamplerState*                     m_pTextureSampler;
    ID3DX11EffectMatrixVariable*            m_pFXWorld;
    ID3DX11EffectShaderResourceVariable*    m_pFXTexture;
    ID3DX11EffectSamplerVariable*           m_pFXTextureSampler;
};

#endif //__SPRITE_H__

Sprite.cpp | I have two comments inserted where the problems start

#include "Sprite.h"
#include "SpriteManager.h"

CSprite::CSprite( STRING _filename )
    : m_nFrameCount( 1 )
    , m_nActualFrame( 0 )
    , m_pTextureSRV( 00 )
    , m_pTextureSampler( 00 )
    , m_pVertices( 00 )
    , m_pFXWorld( 00 )
    , m_pFXTexture( 00 )
    , m_pFXTextureSampler( 00 )
    , m_fFrameDT( 1000.0f )
    , m_fActualFrameDT( 0.0f )
{
    GraphicHelper::CreateSRV( m_pTextureSRV, _filename ); // can't create ID3D11ShaderResourceView

    this->Initialize();
}

int CSprite::Initialize()
{
    D3D11_SAMPLER_DESC samplerDesc = {};
    samplerDesc.AddressU = samplerDesc.AddressV = samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    //  samplerDesc.Filter = D3D11_FILTER_ANISOTROPIC;
    //  samplerDesc.MaxAnisotropy = 4;
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
    HR( theDevice->CreateSamplerState( &samplerDesc, &m_pTextureSampler ) );

    BuildVertices( );

    D3D11_SAMPLER_DESC colorMapDesc = {};
    colorMapDesc.AddressU = colorMapDesc.AddressV = colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    //  colorMapDesc.Filter = D3D11_FILTER_ANISOTROPIC;
    //  colorMapDesc.MaxAnisotropy = 4;
    colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;
    HR( theDevice->CreateSamplerState( &colorMapDesc, &m_pTextureSampler ) );

    m_pFXWorld          = theSpriteManager->GetFX()->GetVariableByName( "gWorld" )->AsMatrix();
    m_pFXTexture        = theSpriteManager->GetFX()->GetVariableByName( "gTexture" )->AsShaderResource();
    m_pFXTextureSampler = theSpriteManager->GetFX()->GetVariableByName( "gTextureSampler" )->AsSampler();

    return 0;
}

void CSprite::SetTexture( STRING _filename )
{
    GraphicHelper::CreateSRV( m_pTextureSRV, _filename );
}

XMMATRIX CSprite::GetWorldMatrix() const
{
    XMMATRIX translation = XMMatrixTranslation( m_position.x, m_position.y, m_position.z );
    XMMATRIX rotationZ = XMMatrixRotationZ( m_fRotation );
    XMMATRIX scale = XMMatrixScaling( m_scale.x, m_scale.y, 1.0f );

    return scale * rotationZ * translation;
}

void CSprite::BuildVertices()
{
    RELEASE_COM( m_pVertices );

    ID3D11Resource* pcolorTex = 00;
    m_pTextureSRV->GetResource( &pcolorTex ); //throw access violation

    D3D11_TEXTURE2D_DESC colorTexDesc = {};
    ( reinterpret_cast<ID3D11Texture2D*>( pcolorTex ) )->GetDesc( &colorTexDesc );
    pcolorTex->Release();

    colorTexDesc.Width /= m_nFrameCount;
    // Calculate corners for uneven boundings
    float left = -(float)(int)( colorTexDesc.Width * 0.5f );
    float right = (float)((int)( colorTexDesc.Width * 0.5f ) );
    if( colorTexDesc.Width % 2 == 1 ) right += 1.0f;
    m_fBoundingLeft = left;
    m_fBoundingRight = right;

    float top = (float)(int)( colorTexDesc.Height * 0.5f );
    float bottom = -(float)(int)( colorTexDesc.Height * 0.5f );
    if( colorTexDesc.Height % 2 == 1 ) bottom -= 1.0f;
    m_fBoundingTop = top;
    m_fBoundingBottom = bottom;

    float FrameWidth = 1.0f / m_nFrameCount;
    // Create Vertexbuffer

    Vertex vertices[] =
    {
        { XMFLOAT3( right, top, 1.0f ),    XMFLOAT2( FrameWidth * ( m_nActualFrame + 1 ), 0.0f ) },
        { XMFLOAT3( right, bottom, 1.0f ), XMFLOAT2( FrameWidth * ( m_nActualFrame + 1 ), 1.0f ) },
        { XMFLOAT3( left, bottom, 1.0f ),  XMFLOAT2( FrameWidth * ( m_nActualFrame     ), 1.0f ) },

        { XMFLOAT3( left, bottom, 1.0f ),  XMFLOAT2( FrameWidth * ( m_nActualFrame     ), 1.0f ) },
        { XMFLOAT3( left, top, 1.0f ),     XMFLOAT2( FrameWidth * ( m_nActualFrame     ), 0.0f ) },
        { XMFLOAT3( right, top, 1.0f ),    XMFLOAT2( FrameWidth * ( m_nActualFrame + 1 ), 0.0f ) },
    };

    D3D11_BUFFER_DESC vdesc = {};
    vdesc.Usage = D3D11_USAGE_IMMUTABLE;
    vdesc.ByteWidth = sizeof( Vertex ) * ARRAYSIZE( vertices );
    vdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    D3D11_SUBRESOURCE_DATA vdata = {};
    vdata.pSysMem = vertices;
    HR( theDevice->CreateBuffer( &vdesc, &vdata, &m_pVertices ) );

    memcpy( &tempVertex, vertices, sizeof( vertices ) );
}

int CSprite::Update( const float _dt )
{
    m_fActualFrameDT -= _dt;
    if( m_fActualFrameDT < 0.0f )
    {
        m_fActualFrameDT = m_fFrameDT;

        int i = m_nActualFrame + 1;
        if( i >= m_nFrameCount ) i = 0;

        SetFrame( i );
    }

    return ERROR_SUCCESS;
}

void CSprite::Render()
{
    UINT stride = sizeof( Vertex );
    UINT offset = 0;
    theContext->IASetVertexBuffers( 0, 1, &m_pVertices, &stride, &offset );
    XMMATRIX world = GetWorldMatrix();
    m_pFXWorld->SetMatrix( reinterpret_cast<float*>( &world ) );
    m_pFXTexture->SetResource( m_pTextureSRV );
    m_pFXTextureSampler->SetSampler( 0, m_pTextureSampler );

    D3DX11_TECHNIQUE_DESC techDesc = {};
    theSpriteManager->GetFXTech()->GetDesc( &techDesc );

    for( UINT p = 0; p < techDesc.Passes; ++p )
    {
        theSpriteManager->GetFXTech()->GetPassByIndex( p )->Apply( 0, theContext );

        theContext->Draw( 6, 0 );
    }
}
Was it helpful?

Solution

Probably you are obtaining this exception because your m_pTextureSRV is NULL, try this:

if (m_pTextureSRV != NULL)
{
    ID3D11Resource* pcolorTex = 00;
    m_pTextureSRV->GetResource( &pcolorTex ); // put a break point here
}

while running, if the break point will stop the execution will mean that I'm wrong but if not that means that I'm right and you haven't initialized m_pTexture correctly!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top