Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:32:44+00:00 2026-06-15T23:32:44+00:00

I had a few problems while programming a small game with DIrectX9. I got

  • 0

I had a few problems while programming a small game with DIrectX9. I got an unhandled exception, and in many attempts I can’t seem to fix it. I would appreciate some insight on this issue. I used the debugger, I just don’t know how to fix the exception.

Output: Unhandled exception at 0x00061afd in AACF.exe: 0xC0000005: Access violation reading location 0x00000000.

It seems the data in the “Data” structure are all equal to 0x00000000; how would I fix this?

The exception is commented in System.cpp.

Exception Location File:

// System.cpp //

#include "Core.h"

struct {
    LPDIRECT3D9 D3D;
    LPDIRECT3DDEVICE9 D3DDEV;
    LPD3DXSPRITE D3DSPT;
    LPD3DXFONT DXFONT;
    LPDIRECT3DTEXTURE9 DisplayTexture;
} Data;

void System::InitD3D(bool Windowed, int ScreenW, int ScreenH) {
    Data.D3D = Direct3DCreate9(D3D_SDK_VERSION);
    D3DPRESENT_PARAMETERS D3DPP;

    ZeroMemory(&D3DPP, sizeof(D3DPP));
    D3DPP.Windowed = Windowed;
    D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
    D3DPP.hDeviceWindow = hWnd;
    D3DPP.BackBufferFormat = D3DFMT_X8R8G8B8;
    D3DPP.BackBufferWidth = ScreenW;
    D3DPP.BackBufferHeight = ScreenH;
    D3DPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

    Data.D3D->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &D3DPP,
                      &Data.D3DDEV);
    D3DXCreateSprite(Data.D3DDEV, &Data.D3DSPT);

    Load_Display();

    return;
}

void System::Render_Frame(void) {
    // EXCEPTIONS START HERE
    // Data.D3DDEV->Clear gives an exception
    Data.D3DDEV->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0F, 0); // Exception line
    Data.D3DDEV->BeginScene();
    Data.D3DSPT->Begin(D3DXSPRITE_ALPHABLEND);

    Draw_Display();

    Data.D3DSPT->End();
    Data.D3DDEV->EndScene();
    Data.D3DDEV->Present(NULL, NULL, NULL, NULL);
    return;
}

void System::CleanD3D() {
    Data.DisplayTexture->Release();
    Data.D3DDEV->Release();
    Data.D3D->Release();
    return;
}

void System::Load_Display() {
    LoadTexture(&Data.DisplayTexture, L"DisplaySprites.png");
    D3DXCreateFont(Data.D3DDEV, 20, 0, FW_BOLD, 1, false, DEFAULT_CHARSET,
                   OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
                   L"Arial", &Data.DXFONT);
    return;
}

void System::Draw_Display() {
    return;
}

void System::LoadTexture(LPDIRECT3DTEXTURE9* Texture, LPCTSTR FileName) {
    D3DXCreateTextureFromFileEx(Data.D3DDEV, FileName, D3DX_DEFAULT, D3DX_DEFAULT,
                                D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
                                D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, Texture);
    return;
}

void System::DrawTexture(LPDIRECT3DTEXTURE9 Texture, RECT TexCoords, float X, float Y, int A) {
    D3DXVECTOR3 Center(0.0F, 0.0F, 0.0F), Position(X, Y, 0.0F);
    Data.D3DSPT->Draw(Texture, &TexCoords, &Center, &Position, D3DCOLOR_ARGB(A,255,255,255));
    return;
}

void System::DrawString(const char* Text, int Count, RECT Rect, float X, float Y, int A, int R, int G, int B) {
    SetRect(&Rect, 0, 0, (int)X, (int)Y);
    Data.DXFONT->DrawTextA(NULL,
                      Text,
                      Count,
                      &Rect,
                      DT_BOTTOM | DT_RIGHT,
                      D3DCOLOR_ARGB(A, R, G, B));
}

void System::DrawNumber(int SetValue, RECT Rect, float X, float Y, int A, int R, int G, int B) {
    SetRect(&Rect, 0, 0, (int)X, (int)Y);
    static char Number[10];
    _itoa_s(SetValue, Number, 10);
    Data.DXFONT->DrawTextA(NULL,
                       (LPCSTR)&Number,
                       strlen((LPCSTR)&Number),
                       &Rect,
                       DT_BOTTOM | DT_RIGHT,
                       D3DCOLOR_ARGB(A, R, G, B));
}

System.cpp Header:

// System.h //

// Include Guard //
#ifndef _SYSTEM_H
#define _SYSTEM_H

#include "Core.h"

// Globalizing Data Structure
struct Data;

// Define screen resolution and keyboard macros
#define SCREEN_WIDTH    (1000)
#define SCREEN_HEIGHT   (700)
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// WinProc ProtoType
LRESULT CALLBACK WindowProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);

class System {
private:
    HWND hWnd;
public:
    System() {};
    void InitD3D(bool Windowed, int ScreenW, int ScreenH);
    void Render_Frame(void);
    void CleanD3D(void);
    void Load_Display();
    void Draw_Display();
    void LoadTexture(LPDIRECT3DTEXTURE9* Texture, LPCTSTR FileName);
    void DrawTexture(LPDIRECT3DTEXTURE9 Texture, RECT TexCoords, float X, float Y, int A);
    void DrawString(const char* Text, int Count, RECT Rect, float X, float Y, int A, int R, int G, int B);
    void DrawNumber(int SetValue, RECT Rect, float X, float Y, int A, int R, int G, int B);
};

#endif

Other Files:

// Core.h //

// Include Guard //
#ifndef _CORE_H
#define _CORE_H

// Pragma Comments
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

// External Include <> //
#include <Windows.h>
#include <WindowsX.h>
#include <d3d9.h>
#include <d3dx9.h>

#include <vector>
#include <map>
#include <iostream>
#include <tchar.h>

// Internal Incldue "" //
#include "System.h"

#endif

// Core.cpp //

#include "Core.h"


int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow) {
    System Sys;
    HWND hWnd;
    WNDCLASSEX WC;

    ZeroMemory(&WC, sizeof(WNDCLASSEX));

    WC.cbSize = sizeof(WNDCLASSEX);
    WC.style = CS_HREDRAW | CS_VREDRAW;
    WC.lpfnWndProc = (WNDPROC)WindowProc;
    WC.hInstance = hInstance;
    WC.hCursor = LoadCursor(NULL, IDC_ARROW);
    WC.lpszClassName = L"WindowClass";

    RegisterClassEx(&WC);
    hWnd = CreateWindowEx(NULL, L"WindowClass", L"Game Name",
                          WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL, NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
    // Set up Direct3D
    Sys.InitD3D(TRUE, SCREEN_WIDTH, SCREEN_HEIGHT);
    // Enter Main Loop
    MSG Msg;
    while(TRUE) {
        DWORD Starting_Point = GetTickCount();
        if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
            if(Msg.message == WM_QUIT)
                break;
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        Sys.Render_Frame();
        // Check escape key
        if(KEY_DOWN(VK_ESCAPE))
            PostMessage(hWnd, WM_DESTROY, 0, 0);
        while((GetTickCount() - Starting_Point) < 25);
    }
    Sys.CleanD3D();
    return Msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    switch(Message) {
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        } break;
    }
    return DefWindowProc(hWnd, Message, wParam, lParam);
}

bool CheckCollision(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh) {
    if(Ay+Ah < By) return false;
    else if(Ay > By+Bh) return false;
    else if(Ax+Aw < Bx) return false;
    else if(Ax > Bx+Bw) return false;

    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:32:46+00:00Added an answer on June 15, 2026 at 11:32 pm

    I’m not an expert on DirectX, but here’s where I would start. First, your InitD3D() should probably return a bool or have a return type other than void because it could fail.

    For starters, I would check to make sure the Create() functions have succeeded. For example, I would probably add something like:

    //Returns NULL pointer on failure.
    Data.D3D = Direct3DCreate9(D3D_SDK_VERSION);
    
    if(!Data.D3D)
        return false;
    

    And when you go to create the device:

    HRESULT hr = Data.D3D->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &D3DPP,
                      &Data.D3DDEV);
    
    if(hr != D3D_OK)
        return false;
    

    I would at least do some basic error checking. If the device is created successfully and has a value at the time of the call, you can rule that out. It’s been a while since I’ve used DirectX so you may have to Release() / clean up some stuff before returning false, but that’s the basic idea.

    It’s easy to check if the device as value at the time of the call. Set a breakpoint on the line where the exception occurs. When you hit it, check the value of Data.D3DDEV in the debugger. If it’s null and has the value 0x00000000 you are trying to deference a null pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so I have had a few small problems while sending data over WiFi.
I had a few problems with my alarm that are in a another thread
I have had a few problems getting this right, so I wanted to ask
I've had quite a few problems with IE6 and our website. http://www.sweetlets.com/w/solutions/click-stream/features/ At the
I have had this problem crop up a few times and I can't figure
I have had the problem a few times now while working on projects and
Hello All at StackOverflow, over the last few days I have had some problems
Hey guys, sorry I'm kind of new to programming, but I had a small
I read about Big-O Notation from here and had few questions on calculating the
I have upgraded my grails app from 1.3.7 to 2.0.1. I had few static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.