locked
Need help with my directx code (0xC0000005: Access violation reading location 0x00000000) RRS feed

  • Question

  • Hello everyone, I really need some help with my directx code since I'm a still a noob when it comes to coding directx. This code that I made gives me an error "0xC0000005: Access violation reading location 0x00000000" and I don't know what causing it and I'm stuck with this damn error for more than a week now. Can anyone help me how to fix my code and get rid of my access error, which line of code should I edit/remove. I really appriciate any help.
    Thnx in advance.

    here's the code

    This one is in uiManager.cpp

    #include "uiManager.h"
    #include "userObj.h"

    userobj *user;

    dxManager:Big SmilexManager(void)
    {   
        pD3D = NULL;
         pd3dDevice = NULL;

    }

    dxManager::~dxManager(void)
    {
    }

    bool dxManager::init(HWND hwnd)
    {
        if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        {
            lastResult = E_FAIL;
            return false;
        }

        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory( &d3dpp, sizeof(d3dpp) );
        d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
        d3dpp.BackBufferCount  = 1;
        d3dpp.BackBufferHeight = 480;
        d3dpp.BackBufferWidth  = 640;
        d3dpp.hDeviceWindow    = hwnd;

        if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,
                                          D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                          &d3dpp, &pd3dDevice ) ) )
        {
            lastResult = E_FAIL;
            return false;
        }

        return true;
    }

    void dxManager:Tongue Tiedhutdown(void)
    {
        if( pd3dDevice != NULL)
        {
            pd3dDevice->Release();
            pd3dDevice = NULL;
        }
        if( pD3D != NULL)
        {
            pD3D->Release();
            pD3D = NULL;
        }
    }

    void dxManager::beginRender()
    {
        if( NULL == pd3dDevice )
            return;

        // Clear the backbuffer to a blue color
        pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );

        user->Draw_sprite();
    }

    void dxManager::endRender(void)
    {
        // Present the backbuffer contents to the display
        pd3dDevice->Present( NULL, NULL, NULL, NULL );
    }

    IDirect3DSurface9* dxManager::getSurfaceFromBitmap(std:Tongue Tiedtring filename)
    {
        HRESULT hResult;
        static IDirect3DSurface9* surface = NULL;
        D3DXIMAGE_INFO imageInfo;

        // Get the width and height info from this bitmap
        hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
        if FAILED (hResult)
            return NULL;


        hResult = pd3dDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
        if (FAILED(hResult))
            return NULL;

        hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL);
        if (FAILED(hResult))
            return NULL;

        return surface;
    }

    IDirect3DSurface9* dxManager::getBackBuffer(void)
    {
        IDirect3DSurface9* backbuffer = NULL;

        if (!pd3dDevice)
            return NULL;

        HRESULT hResult = pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);
        if (FAILED(hResult))
            return NULL;
        else
            return backbuffer;
    }

    void dxManager::blitToSurface(IDirect3DSurface9* srcSurface, const RECT *srcRect, const RECT *destRect)
    {
        pd3dDevice->StretchRect(srcSurface, srcRect, getBackBuffer(), destRect, D3DTEXF_NONE);
    }

    This one is in the userObj.cpp

    #include ".\uiManager.h"
    #include "userObj.h"

    #define MAX_SPRITES 3           // this is the number of sprites we want
    #define SPRITE_WIDTH 90            // the width of all sprites
    #define SPRITE_HEIGHT 100        // the height of all sprites

    // this is the sprite structure
    struct {
        RECT srcRect;                // holds the location of this sprite
                                    // in the source bitmap
        int posX;                    // the sprites X position
        int posY;                    // the sprites Y position
    } spriteStruct[MAX_SPRITES];

    /* Get a reference to the UserInterface Manager */   
    static dxManager& uiMgr = dxManager::getInstance();

    userobj::userobj(void)
    {
        //spriteSurface = NULL;
    }

    userobj::~userobj(void)
    {
    }

    void userobj:Big Smileraw_sprite(void)
    {
        for (int i=0; i < MAX_SPRITES; i++)
                {               
                    RECT destRect;
                    destRect.left = spriteStructIdea.posX;
                    destRect.top = spriteStructIdea.posY;
                    destRect.bottom = destRect.top + (SPRITE_HEIGHT);
                    destRect.right = destRect.left + (SPRITE_WIDTH);
                    uiMgr.blitToSurface(spriteSurface, &spriteStructIdea.srcRect, &destRect);
                }
    }

    bool userobj::initSprites(void)
    {
        spriteSurface = uiMgr.getSurfaceFromBitmap("3Tabs.bmp");
        if (spriteSurface == NULL)
            return false;

        for (int i=0; i < MAX_SPRITES; i++)
        {
            spriteStructIdea.srcRect.top = 0;
            spriteStructIdea.srcRect.left = i * SPRITE_WIDTH;
            spriteStructIdea.srcRect.right = spriteStructIdea.srcRect.left + SPRITE_WIDTH;
            spriteStructIdea.srcRect.bottom = SPRITE_HEIGHT;
            spriteStructIdea.posX = 370 + (i * SPRITE_WIDTH);
            spriteStructIdea.posY = 0;
            //spriteStructIdea.posY = i * SPRITE_HEIGHT;
        }

        return true;
    }

    Saturday, February 2, 2008 9:40 AM

All replies

  • You need to at least give us a line number where this error is occuring.  From the looks of it, though, what you're dealing with is a null pointer exception.

    Saturday, February 2, 2008 2:23 PM
  • Sorry about that.

    Here is the line where the error occurs:

    uiMgr.blitToSurface(spriteSurface, &spriteStructIdea.srcRect, &destRect);

    it's under the Draw_sprite function.
    Sunday, February 3, 2008 12:51 AM
  •  Meguri wrote:
    uiMgr.blitToSurface(spriteSurface, &spriteStruct.srcRect, &destRect);

    I assume that one of those parameters are zero; which one? Probably spriteSurface, and I don't see a definition, declaration or even an initialization of that, so if it is the problem, it is unlikely anyone can help. I am not familiar with DirectX enough to be able to help, but if you need help using that function, you will hopefully get help if you can be specific about what is unclear. To get help with DirectX, you might need to use a DirectX newsgroup or a forum that specializes in DirectX in another site.

    Sunday, February 3, 2008 4:39 AM