Skip to main content
gamedev.net
🔒 Locked

Access Violation?

Started by Scar 6 replies 700+ views
Original Post
Scar
Scar
I've been working on a game engine for the past 2 weeks, I've run into some wierd errors throughout my time, but this one takes the pie. I've debugged it from start to finish, watching the addresses carefully. I noticed that the function that causes the error SETS an argument to 0x00000000 or NULL and that seems to be causing the access violation. I watched that argument all the way down to the function and it's definitely not NULL. Here's some code: Error Function

BOOL ArkButton::Init(ArkCore *Ark, LPCTSTR TextureFilename, int MaxButtons)
{
	if( Ark == NULL || TextureFilename == NULL || MaxButtons == NULL )
		return FALSE;
	if( Ark->GetDevice() == NULL )
		return FALSE;

	*m_Button = new sButton[MaxButtons];

	if(FAILED(D3DXCreateTextureFromFile(Ark->GetDevice(), TextureFilename, &m_Texture)))  // ACCESS VIOLATION, ARK IS THE SUPPOSED NULL OBJECT(NOTE THE CHECK FOR IT)
		return FALSE;
And the call:

/* PRE INSTANTIATED OBJECTS **********
ArkCore* AC;
ArkButton* AB;
**********************************/

AB->Init(AC, "C:\\UI\\Buttons.jpg", 2); // The call
I'm stumped, any ideas?
ggambett
ggambett
I don't see anything that obviously causes the crash, but

1) It doesn't make much sense to check MaxButtons == NULL since MaxButtons isn't a pointer. Sure, NULL is 0 so it's perfectly valid for the compiler, but it's kinda ugly for a human.

2) Why are you dereferencing m_Button? Is it really a sButton**? If it is, have you allocated memory for it?
JoeyBlow2
JoeyBlow2
Can you please link the source for Ark->GetDevice()?

Even though the pointer to Ark is valid, doesn't mean the pointer returned by the GetDevice function is. A pointer can contain an invalid reference (in other words, a number that is not NULL) if it wasn't properly initialized.
JoeyBlow2
JoeyBlow2
Oh yeah.

You have

ArkButton* AB;

Are you ever doing a new on that?

AB = new ArkButton;

And the AC?


Scar
Scar
Quote:

I don't see anything that obviously causes the crash, but

1) It doesn't make much sense to check MaxButtons == NULL since MaxButtons isn't a pointer. Sure, NULL is 0 so it's perfectly valid for the compiler, but it's kinda ugly for a human.

2) Why are you dereferencing m_Button? Is it really a sButton**? If it is, have you allocated memory for it?


1: My apologies if my NULL confused ya. Tis a habit :)

2: m_Button is infact **. Thank you for pointing that out, it's wierd how the compiler chose the line after that one to throw the violation but it was infact the m_Button line. I changed **m_Button to *m_Button and changed everything accordingly and it now runs smoothly. However, I would like to know why I couldn't assign a memory allocation to a dereferenced double pointer. It seems like it should work to me since you can dereference a pointer and change the value of the pointed address directly. Anyway, Thanks for the help, everything works now :)
Scar
Scar
Quote:
Original post by JoeyBlow2
Oh yeah.

You have

ArkButton* AB;

Are you ever doing a new on that?

AB = new ArkButton;

And the AC?


Yes sir, everything was instantiated. Thanks for the reply, It works now :)
TDragon
TDragon
Quote:
Original post by Scar
However, I would like to know why I couldn't assign a memory allocation to a dereferenced double pointer. It seems like it should work to me since you can dereference a pointer and change the value of the pointed address directly.

Any pointer is invalid until you assign it an address. For double pointer indirection, you have to assign two addresses. In this case, you would have had to use "m_Button = new sButton*;", which is obviously pointless (no pun intended). Removing one of the layers of indirection and the dereference was the right way to go.

Cheers,
Twilight Dragon
ggambett
ggambett
Quote:
It seems like it should work to me since you can dereference a pointer and change the value of the pointed address directly. Anyway, Thanks for the help, everything works now :)

You can. But when you have a pointer to a pointer, you have to allocate memory for the pointer, and then allocate memory for the object it will point to! Let's do a diagram :

pointer to Object ---> (NULL)

Then you do pointer = new Object();

pointer to Object ---> Object

Now when you declare a Object** you have a pointer to a pointer to an object. So...

pointer to pointer to Object ---> (NULL)

You need to allocate memory for the pointer - pointer = new (Object*)

pointer to pointer to Object ---> pointer to Object ---> (NULL)

And now you can actually use that pointer - (*pointer) = new Object

pointer to pointer to Object ---> pointer to Object ---> Object

To make it clearer, use a typedef - typedef Object* ObjectPtr

ObjectPtr* pPtr = new ObjectPtr;
*pPtr = new Object;

Hope that helps!
--G

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.