Sign in to follow this  
Tizela

D3D9 Font C++. In need of help.

Recommended Posts

Hey, been scouting the forums for some time when I've been stuck but never signed up before. And I have a little "Access violation problem", I am a beginner of both c++ and dx9 so it's probably nothing hard. Thanks in advance. First, my Font class.
class dmText
{
public:
dmText();
~dmText();
void CreateText();
void NullFont();
void RenderText(int x,int y, LPCWSTR dmString);

private:
static LPD3DXFONT dmFont;
LPCWSTR Text;
};



I use CreateText to create the Font. RenderText to render Text and NullFont to rlease it after it being done rendering. (I havn't added all functions I want yet so don't diss it completely ;) )
void dmText::CreateText()
{
D3DXFONT_DESC FontDesc = {24,
                          0,
                          400,
                          0,
                          false,
                          DEFAULT_CHARSET,
                          OUT_TT_PRECIS,
                          CLIP_DEFAULT_PRECIS,
                          DEFAULT_PITCH,
                          TEXT("Arial")};


D3DXCreateFontIndirect(dx_d3dDevice,&FontDesc,&dmFont);
}


void dmText::RenderText(int x,int y, LPCWSTR dmString)
{

RECT fontPlace = {x,y,800,600};


 dmFont->DrawText(NULL,
                   dmString,
                   -1,
                   &fontPlace,
                   DT_CENTER,
                   0xffffffff); 
}

void dmText::NullFont()
{

if(dmFont != NULL)
 {
 dmFont->Release();
 dmFont = NULL;
 }



That's that. It's corectly set up, and it's then renderd. After adding brakepoints and watching it, it gets renderd corectly once, then it crashes selecting.
pmsText.RenderText(10,3,TEXT("yay"));
 


In this function.
void MainRender()
{


dx_d3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(80,120,50), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( dx_d3dDevice->BeginScene() ) )
    {
pmsText.RenderText(10,3,TEXT("yay"));


// End the scene
    dx_d3dDevice->EndScene();
	}
pmsText.NullFont();


    dx_d3dDevice->Present( NULL, NULL, NULL, NULL );
}



All Lib's and headers included.
#include <Windows.h>
#include "d3dx9.h"
#include <mmsystem.h>
#pragma warning( disable : 4996 )
#include <strsafe.h>
#pragma warning( default : 4996 ) 
#include "dinput.h"
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"dxerr9.lib")
#pragma comment(lib,"dinput8.lib")
#pragma comment(lib,"dxguid.lib")
#pragma comment(lib,"dinput.lib")



I guess it has something to do with it not getting released and nulled right, but after messing with it for a good while I figured that I'm in need of help. Thanks a lot in advance. EDIT: Exact error message. Unhandled exception at 0x00401688 in DM.exe: 0xC0000005: Access violation reading location 0x00000000.

Share this post


Link to post
Share on other sites
That error is telling you that you are trying to acces some object that is pointing to 0x0000. This implies one of 2 things. Either you are incorrectly initializing some object (check error messages), are you destroy an object then try to use it again.

Either way, you need to make sure you are running with the debug runtimes and read your DirectX error messages. These will be much more specific and will give yo VERY good idea why your functions are failing.

Share this post


Link to post
Share on other sites
pmsText.NullFont() is destroying the internal dmFont object, then the next time you execute MainRender and try to call pmsText.RenderText, it will attempt to dereference dmFont (which has been set to NULL) and this results in an access violation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this