pointer problem

Started by
7 comments, last by Adam_42 14 years, 10 months ago
I have a problem with a pointer data member in a class. I have a class called Texture that has a pointer to a Direct3DTexture9 ( LPDIRECT3DTEXTURE9 pd3dTex). I also have a graphics controller class with this method: Texture* CreateTexture( //wateva ) { Texture* temp = new Texture(); temp->Initalise( //this calls D3DXCreateTextureFromFileExA ) return temp; } Now, if I do this: Texture* myTexture = graphicsControl->CreateTexture( //... ); I get an access violation for pd3dTex. But I can do this: Texture* myTexture = new Texture(); and everythis is dandy. So there is obviously somethig wrong happening in CreateTextre(), but i have no idea what.
Advertisement
Please post the complete relevant code. Without being able to see what your code is actually doing, we can't exactly figure out what it's doing wrong [wink]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Sure, sorry.

// main.cpp --------------------------------------------

Engine engine;

GraphicsControl* gc = engine.CreateGraphicsCotrol();

//GraphicsControl* Engine::CreateGraphicsControl()
//{
//graphicsControl = new GraphicsControl();
//return graphicsControl;
//};

Texture* mytexture = gc->CreateTexture( const char* pSrcFile, D3DCOLOR colourKey );

//Texture* GraphicsControl::CreateTexture( const char* pSrcFile, D3DCOLOR //colourKey )
//{
//Texture* temp = new Texture();
//temp->Initialise( d3ddev, pSrcFile, colourKey );

//return temp;
//}

Access violation reading location 0xcdcdcdcd. In call to D3DXCreateTextureFromFileExA within Texture::Initialise()

I pass the LPDIRECT3DTEXTURE9 (member variable of Texture class) into
D3DXCreateTextureFromFileExA.

But it all works if i create the texture directly dinamically:

Texture* mytex = new Texture();

Hope thats clear.
Try this: Google search of 0xcdcdcdcd.

Such patterns in pointers are often indicative of a "special" value used by the debugger.
Thanks,

Apparently 0xcdcdcdcd is assigned when the object hasnt been initialised.

But why does the LPDIRECT3DTEXTURE9 have to be initialised before being passed into D3DXCreateTextureFromFileExA.

I dont even know how to initialise a LPDIRECT3DTEXTURE9, or that you can.
That would depend on the exact types of the variables involved and what the calls look like. In other words, you haven't shown enough code.
class Texture{        private:            LPDIRECT3DTEXTURE9 m_pTexture;        public:            Texture();            ~Texture();            int Initialise( LPDIRECT3DDEVICE9 device, const char* srcFile, D3DCOLOR colourKey );            int Dispose();            LPDIRECT3DTEXTURE9 GetPointer();};Texture::Texture()        {           m_pTexture = NULL;        };        Texture::~Texture()        {        };        int Texture::Initialise( LPDIRECT3DDEVICE9 device, const char* srcFile, D3DCOLOR colourKey )        {            if( m_pTexture )            {                m_pTexture->Release();                m_pTexture = NULL;            }            		    if( FAILED( D3DXCreateTextureFromFileExA(device, srcFile,                                                         D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,                                                         NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,                                                         D3DX_DEFAULT, D3DX_DEFAULT,                                                         colourKey,                                                         NULL, NULL,                                                         &m_pTexture) ) )                {                    return false;                }             return true;                     };        int Texture::Dispose()        {            m_pTexture->Release();            m_pTexture = NULL;            return true;        };// Graphics control classclass GraphicsControl{public:	GraphicsControl();        ~GraphicsControl();        Texture* CreateTexture( const char* pSrcFile, D3DCOLOR colourKey );private:	LPDIRECT3D9         d3d;	LPDIRECT3DDEVICE9   d3ddev;	LPDIRECT3DSURFACE9  depthBuffer;        LPD3DXSPRITE        d3dxsprite;};Texture* GraphicsControl::CreateTexture( const char* pSrcFile, D3DCOLOR colourKey ){        Texture* temp = new Texture();        temp->Initialise( d3ddev, pSrcFile, colourKey );        return temp;}// In main.cppEngine              g_engine;GraphicsControl*    g_gc = g_engine.CreateGraphicsControl();Texture*            g_tex1 = g_gc->CreateTexture( "tex.bmp", D3DCOLOR_XRGB(0,0,0) );// Later I call g_tex1->Dispose();


[Edited by - haywire on June 7, 2009 11:26:51 PM]
Quote:// Later I call
g_tex1->Dispose();


Are you sure that you call it at an appropriate time?

BTW, this is what destructors are for. You can't possibly forget to call a destructor, because it's called automatically. With a Dispose() method, you have to think about when to call it (and it can be very tricky, especially taking exceptions into consideration).

Oh, and while I'm pointing things out: C++ has a perfectly good boolean type, named 'bool', which is the type of the constants 'true' and 'false'. You should use it as the type of the variable (or parameter, or return value) to which you assign those constants. And there's no point in having a function return a boolean value if it will always return true (or if it will always return false). In C++, we use 'void' as a return type for functions that do not have anything meaningful to return.
I believe happening here is that your device creation code is failing and returning an error code.

When it fails it doesn't create a device, and your device pointer remains uninitialized.

D3DX then tries to use that uninitialized device pointer and crashes.

I'd start by making use of the debug runtimes (turn them on in the control panel installed with the SDK), as they will give you plain English error messages in the debug output.

This topic is closed to new replies.

Advertisement