-
July 27th, 2009, 03:39 PM
#1
Unable to access pointer
Hi all,
I seem to be unable to access member pointers to structs. Here are the struct declarations in the class:
Code:
SpriteStruct* first;
SpriteStruct* last;
Any time I try to access first or last, I get an access violation at 0x00000000. Examples:
Code:
first = NULL;
if (first==NULL);
Thank you in advance for any information regarding this problem!
-
July 27th, 2009, 03:42 PM
#2
Re: Unable to access pointer
Originally Posted by hockeyb
Hi all,
I seem to be unable to access member pointers to structs. Here are the struct declarations in the class:
Your program must be much longer than 4 lines.
Please post the actual code that duplicates the error. Setting a pointer to NULL and comparing it to NULL does not cause errors by itself -- your code is doing much more to cause the problem than what you've shown.
Regards,
Paul McKenzie
-
July 27th, 2009, 03:50 PM
#3
Re: Unable to access pointer
That's what my team and I also thought at first, but this is the isolated portion of the code that causes the error. I'm sorry I wasn't clear, but the two statements in the bottom code section are in separate methods. I took out the first line after I saw that was where the access violation break point was and the second line also caused the same error at the same location.
Here's the class:
Code:
#ifndef DIRECT2D_SOFTWARE_DEVICE
#define DIRECT2D_SOFTWARE_DEVICE
#include "windows.h"
#include "windowsx.h"
#include "d3d9.h"
#include "d3dx9.h"
#include "d3dx9math.h"
#include "TerraEngine.h"
#include <vector>
using namespace std;
#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")
class Direct2DSoftwareDevice
{
public:
void Start();
void SpriteToDrawBuffer(LPDIRECT3DTEXTURE9 inSprite,
D3DXVECTOR3 inPosition,
D3DXVECTOR3 inCenter,
D3DCOLOR inColor,
RECT* inSrcRect,
LPDIRECT3DDEVICE9 D3DDevice);
void RenderDrawBuffer();
void Clean();
private:
//Keeps information about a sprite to be drawn
//and implements a linkedlist structure
struct SpriteStruct
{
LPD3DXSPRITE D3DSprite;
RECT* pSrcRect;
D3DXVECTOR3 Position;
D3DXVECTOR3 Center;
D3DCOLOR Color;
LPDIRECT3DTEXTURE9 SpriteTexture;
SpriteStruct* next;
};
//Sprite draw buffer
SpriteStruct* first;
SpriteStruct* last;
};
#endif
And the first method that was causing the error:
Code:
void Direct2DSoftwareDevice::Start()
{
//!!!!!!!!!!
//DIES HERE
//!!!!!!!!!!
first = NULL;
last = NULL;
}
And the second method that was causing the error:
Code:
void Direct2DSoftwareDevice::SpriteToDrawBuffer(LPDIRECT3DTEXTURE9 inSprite,
D3DXVECTOR3 inPosition,
D3DXVECTOR3 inCenter,
D3DCOLOR inColor,
RECT* inSrcRect,
LPDIRECT3DDEVICE9 D3DDevice)
{
LPD3DXSPRITE d3dspt;
D3DXCreateSprite(D3DDevice, &d3dspt);
SpriteStruct ss;
ss.D3DSprite = d3dspt;
ss.Center = inCenter;
ss.Color = inColor;
ss.Position = inPosition;
ss.pSrcRect = inSrcRect;
ss.SpriteTexture = inSprite;
ss.next = NULL;
//!!!!!!!!!!
//DIES HERE
//!!!!!!!!!!
if (first==NULL)
{
first = &ss;
last = &ss;
} else
{
SpriteStruct* oldLast = last;
last = &ss;
oldLast->next = last;
}
SpriteStruct st;
}
-
July 27th, 2009, 03:58 PM
#4
Re: Unable to access pointer
This probably has nothing to do with the fact that the members you're accessing are pointers. Notice that in both cases, the code dies the first time any member of the Direct2DSoftwareDevice class is touched.
This probably means that you're trying to call these methods on something which isn't a valid Direct2DSoftwareDevice object. Usually this can happen if you're trying to access the object via an invalid Direct2DSoftwareDevice*.
-
July 27th, 2009, 04:06 PM
#5
Re: Unable to access pointer
Set a breakpoint at two positions, such that the debugger stops at, say:
Code:
void Direct2DSoftwareDevice::Start()
{
//!!!!!!!!!!
//DIES HERE
//!!!!!!!!!!
first = NULL; //<< so debugger stops here
last = NULL;
}
Then, tell us what the value of "this" is.
Then, look at the call stack and find the code where "Start" is called
..from there, look for the object upon whose instance Start is called,
and look further backwards until you find where that object is created.
Tell us the code you have there.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
July 27th, 2009, 04:29 PM
#6
Re: Unable to access pointer
Thank you Lindley! You were spot on. A pointer to an instance of the class wasn't properly initialized. Doh. Thank you JVene for your help as well. I'll try to remember to make the instance before I use its non-static methods next time. =)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|