Sign in to follow this  
zealotgi

C++ Class Problems

Recommended Posts

zealotgi    100
I created a class used for making particles, but when I declare it, the following run-time exception comes up:
Quote:
Unhandled exception at 0x00407f41 in Game.exe: 0xC0000005: Access violation reading location 0xccccccd0.
This comes up whenever I declare it Can anyone give me some strategies to help me solve this problem? (I mean, in general, because I know that nobody can read the hex and know whats going on)

Share this post


Link to post
Share on other sites
Motz    136
Could you possibly post the class's constructor and the context within which it is initialised? Might be easier to understand then (I'm guessing it's a memory management issue).

Share this post


Link to post
Share on other sites
OrangyTang    1298
Adresses like 0xCCCCCCCC etc. tend to be special codes inserted by the compiler in debug builds (that one being uninitialised memory (in VS6) if I remember right). Look up the hex codes for your compiler for a start.

Probably caused by a bad pointer to/in your newly defined class.

Share this post


Link to post
Share on other sites
Zahlman    1682
0xcccccccc is a fill pattern used by the compiler in debug mode to flag bits of memory that were not initialized properly (IIRC). If you are using pointers, be aware that declaring a pointer doesn't declare a pointed-at thing; you must point the pointer at an instance of the appropriate type (either a new one or a previously existing one).

Share this post


Link to post
Share on other sites
zealotgi    100
Okay, well here is what I have for the class. I'm not really sure if this will help, but maybe you can help me through this



const int MAX_PARTICLES = 100;

typedef struct sParticle
{
int Size;
float LifeTime;
D3DXVECTOR3 CurPos;
D3DXVECTOR3 CurVel;
D3DCOLOR Color;
BOOL Enabled;
} sParticle;

class cParticle
{
private:
IDirect3DDevice9 *m_pD3DDevice;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer;
LPDIRECT3DTEXTURE9 g_pTexture;
inline DWORD FtoDW( FLOAT f ) { return *((DWORD*)&f); }

sParticle gParticle[MAX_PARTICLES];

public:

BOOL InitParticles(char *Filename);
BOOL UpdateParticles();
BOOL RenderParticles();

void SetEmitter(int Number, float XPos, float YPos, float ZPos);
void SetVelocity(int Number, float XPos, float YPos, float ZPos);
void SetLifeTime(int Number, float Seconds);
void SetColor(int Number, DWORD Color = D3DCOLOR_RGBA(255,255,255,255));
void SetSize(int Number, float Size = 1.0f);
void Enable(int Number, BOOL Enable = TRUE);
};



EDIT: Forgot some essentials

Share this post


Link to post
Share on other sites
Benjamin Heath    925
How are you declaring it? If you create a new cParticle object on the heap, based on the code you've shown us so far, it'll just call its do-nothing default constructor. Assuming InitParticles() is called afterward, that's the first place I'd look for the problem.

Share this post


Link to post
Share on other sites
zealotgi    100
Well, I attempted to compile it without InitParticles() and it still does the same thing, so I'm pretty sure its the declaration that is causing the problem. I do have another class that is using the *m_pD3DDevice pointer, but that shouldn't cause a problem, should it?

I am using the Game Core from Jim Adams book "Programming Role Playing Games in DirectX", if that helps.

Share this post


Link to post
Share on other sites
zealotgi    100
Okay, I got it to work. The problem was that I was declaring it before other members of the application class.

I have another problem, this one is with InitParticles().

Does this code look abnormal to anyone?



// IDirect3DDevice9 *g_pD3DDevice;
// LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer;
// LPDIRECT3DTEXTURE9 g_pTexture;
// Vertex *pPointVertices;

BOOL cParticle::InitParticles(char *Filename)
{

D3DXCreateTextureFromFile(g_pD3DDevice, Filename, &g_pTexture );

g_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

// Create a vertex bufer which can be used with point sprites...

g_pD3DDevice->CreateVertexBuffer( 2048 * sizeof(Vertex), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY | D3DUSAGE_POINTS,
Vertex::FVF_Flags, D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL );

// See the the max size that a point sprite can be set to,
// and whether or not you can change the size of point sprites in hardware
// by sending D3DFVF_PSIZE with the FVF

float fMaxPointSize = 0.0f;
bool bDeviceSupportsPSIZE = false;

D3DCAPS9 d3dCaps;
g_pD3DDevice->GetDeviceCaps(&d3dCaps);

fMaxPointSize = d3dCaps.MaxPointSize;

if( d3dCaps.FVFCaps & D3DFVFCAPS_PSIZE )
bDeviceSupportsPSIZE = true;
else
bDeviceSupportsPSIZE = false;

return TRUE;
}




Quote:

Unhandled exception at 0x0041fbfb in Game.exe: 0xC0000005: Access violation reading location 0x00000000.

Share this post


Link to post
Share on other sites
Sneftel    1788
You really need to learn how to use your debugger. The debugger will tell you exactly what line the problem occurs on, what caused the problem, what all the variables are set to at the time, and what stack of functions led to the problem occurring.

Share this post


Link to post
Share on other sites
zealotgi    100
Quote:
Original post by twkr
probably one of your variables are null.


Well, I do set g_pD3DDevice to NULL in the Constructor. That is proper, isn't it, since it is a pointer, that is?

Share this post


Link to post
Share on other sites
Dave Hunt    4872
Quote:
Original post by zealotgi
Quote:
Original post by twkr
probably one of your variables are null.


Well, I do set g_pD3DDevice to NULL in the Constructor. That is proper, isn't it, since it is a pointer, that is?


Yes, but did you subsequently create the device and give it a value (and verify it)?

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