Archived

This topic is now archived and is closed to further replies.

How to stop dx method from crashing?

This topic is 5143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ive got a directx font class that ive made, used to create font types.
	class Font {
	public:
		LPD3DXFONT lp_Font;
		Font(){}

		Font(char font[], int size, int weight,bool underline, bool italic)
		{
			HDC hDC= GetDC( NULL );
			int nHeight= -( MulDiv( size, GetDeviceCaps(hDC, LOGPIXELSY), 72 ) );
			ReleaseDC( NULL, hDC );					
			HFONT hFont = CreateFont(nHeight, 0, 0, 0, weight, italic, underline, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, font );
			D3DXCreateFont(lp_Device , hFont, &lp_Font);
			DeleteObject( hFont );	
		}
		
		~Font()
		{
			if (lp_Font!=NULL)
				 lp_Font->Release();
		}
};
the problem is when im using it like so:
Font fntNormal;
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{	
        initD3DDevice();
	fntNormal=  Font("Arial", 12, 300, false, false);
	return 0;
}

The problem is the Font("Arial... bit makes it create a new instance and deletes it while fntNormal is still using it causing it to crash...getting access violation error I dont want to use pointers as i dont want to have to bother deleting them, and i dont really want to have a method in there eg create() as this will be the style for alot of other classes that i may use. So i dont want to have to know functions etc. Are there any other ways of getting this working? Thanks,

Share this post


Link to post
Share on other sites
my first guess would be that you have to declare a copy-constructor in the class:


class Font
{
public
...
Font( const Font& F )
{
// do a deep copy, create new lp_Font object
// and set it''s properties to the ones F had.
}
};


By default, the copy-constructor doesn''t do a deep copy, but only copies the variables or pointer addresses.

Share this post


Link to post
Share on other sites