Page 1 of 2

ITexture is giving Access Violation Error

Posted: Wed May 24, 2006 7:53 pm
by Xharock
OK so I want to load a texture and display it on the screen. In a header file I have the following line:

Code: Select all

//Background
extern ITexture *BGTex;
extern IGUIImage *BG;
This throws up no errors and is OK. It is the following line in a .CPP file that gives me the error.

Code: Select all

ITexture *BGTex = Driver->getTexture("2D Images/Menu/BG.bmp");
(The above line is contained inside a function initMenus() which is called in the main() function)

Giving me this error:

Code: Select all

Unhandled exception at 0x0041205d in Program.exe: 0xC0000005: Access violation reading location 0x00000000.
I'm not quite sure why. Can anyone help?

Posted: Wed May 24, 2006 8:24 pm
by TheGameMaker
maybe, that it got some problem with the space in "2D Images"

anyway, it looks like your pointer (BGtex) isnĀ“t filled correctly, cause it shows to the address 0x00000000. so there is an error loading the img data....

Posted: Wed May 24, 2006 9:05 pm
by hybrid
If the line presented is really the one causing the error then your Device pointer is not correctly initialized. Maybe you initialize it somewhere else and use a local variable in this class?

Posted: Thu May 25, 2006 6:25 am
by Xharock
I initialise the Devices and Drivers in a function called initSystems(). It's called before initMenu() but if I don't re-initialise them in the same source as the initMenu() function I get the Error Unresolved External blah error. If I have to initialise it in each source code that kid of defeats the point of having a function to do it.

Posted: Thu May 25, 2006 8:14 am
by hybrid
No, there's definitely something wrong with that. Cannot tell exact reason without seeing the code, but you're using a wrong devie pointer here for sure.

Posted: Thu May 25, 2006 8:29 am
by Xharock
OK basically I want to decalre the IrrlichtDevice *MainDevice in a header file using the extern keyword and then to define it inside a function in a .CPP file. I then want the device to be accessible to all other files that need it. However at the moment I'm having to re-define the MainDevice pointer in each file that uses it. Global MainDevice is what I'm after in a nutshell. I can't post code because I'm at school but I will later if needed.

Posted: Thu May 25, 2006 12:17 pm
by hybrid
Since it's only a pointer there's no problem with redefining it everywhere, but you have to assign all pointers the same correct value. So after calling createDevice you should pass on this pointer to the methods that need access to the device. I think this is a better way than declaring it global. Global variables should still work, though, jus be sure that you use the same namespaces etc.

Posted: Thu May 25, 2006 2:54 pm
by Xharock
Ahhh this is so annoying! I have created the device fine but it's still saying theres somethnig wrong with the line ITexture *BGTex = Blah;

Posted: Thu May 25, 2006 3:39 pm
by vitek
Use your debugger. Set a breakpoint on the line that gives you problems. Inspect the variables to figure out what is going on.

Travis

Posted: Thu May 25, 2006 4:02 pm
by Xharock
I've just seen something:

Code: Select all

Driver	0x003e0f70	irr::video::IVideoDriver *


irr::IUnknown	{ReferenceCounter=12 DebugName=0x00000000 <Bad Ptr> }	irr::IUnknown
I'm probably totally wrong but from the looks of it I'm doing something wrong with my IVideoDriver Pointer.

Code: Select all

IVideoDriver *Driver = MainDevice->getVideoDriver();
That's how I'm defining it.

Re: ITexture is giving Access Violation Error

Posted: Thu May 25, 2006 5:24 pm
by Acki
Xharock wrote:

Code: Select all

ITexture *BGTex = Driver->getTexture("2D Images/Menu/BG.bmp");
You declare BGTex in this function, so it doen't affect your global BGTex !!!
Try it like this:

Code: Select all

BGTex = Driver->getTexture("2D Images/Menu/BG.bmp");
And don't forget to declare BGTex global !!!

Posted: Thu May 25, 2006 5:28 pm
by Xharock
I can't do that otherwise I get linker errors saying the symbol is unresolved.

EDIT: I've re-arranged my code now. All function declarations are now in one header so only things that are needed to be global are. But I still get the error.

Posted: Thu May 25, 2006 5:52 pm
by Acki
Well, I'm sure the error has something to do with this...
Maybe you should post the relevant code...

If you declare a var as extern it has to be declared global somewhere else...
extern is no declaration it just says that the var is somwhere else declared...
If you declare a var with the same name inside a function (like you did) there are 2 single vars with the same name (one global and one local), inside this function only the local var is affected and the global still remains 0 (NULL)...

Posted: Thu May 25, 2006 6:03 pm
by Xharock
Here is the definition of my Irrlicht Stuff:

Code: Select all

EventReceiver EventRec;

IrrlichtDevice *MainDevice = createDevice(EDT_DIRECT3D9, dimension2d<s32>(800, 600), 16, false, true, false, &EventRec);
ISceneManager *Scene = MainDevice->getSceneManager();
IVideoDriver *Driver = MainDevice->getVideoDriver();
IGUIEnvironment *GUI = MainDevice->getGUIEnvironment();
and in the same file my iniMenu() function:

Code: Select all

int initMenu()
{
	cout<<"Initialising Menus...\n\n";

	BGTex = Driver->getTexture("2D Images/Menu/BG.bmp");
	
	if(!BGTex)
	{
		cout<<"Error Initialising Menus\n\n";
		MessageBoxA(NULL, "Could not create menu", "Menu Initialisation Error", MB_OK|MB_ICONERROR);
		return 0;
	}

	IGUIImage *BG = GUI->addImage(BGTex, position2d<s32>(0, 0));

	cout<<"Menus Initialised OK\n\n";
	return 1;
}
Elsewhere in a header file...:

Code: Select all

extern IrrlichtDevice *MainDevice; //The Main Irrlicht Device
extern ISceneManager *Scene; //Scene Manager
extern IVideoDriver *Driver; //Video Driver
extern IGUIEnvironment *GUI; //GUI Environment

Posted: Thu May 25, 2006 7:43 pm
by Acki
Hmmm, and where do you declare BGTex ????
I mean not loading the bitmap into it...

Is this a global declaration ???

Code: Select all

EventReceiver EventRec;

IrrlichtDevice *MainDevice = createDevice(EDT_DIRECT3D9, dimension2d<s32>(800, 600), 16, false, true, false, &EventRec);
ISceneManager *Scene = MainDevice->getSceneManager();
IVideoDriver *Driver = MainDevice->getVideoDriver();
IGUIEnvironment *GUI = MainDevice->getGUIEnvironment();
If so, add the declaration for the texture to it:

Code: Select all

EventReceiver EventRec;

IrrlichtDevice *MainDevice = createDevice(EDT_DIRECT3D9, dimension2d<s32>(800, 600), 16, false, true, false, &EventRec);
ISceneManager *Scene = MainDevice->getSceneManager();
IVideoDriver *Driver = MainDevice->getVideoDriver();
IGUIEnvironment *GUI = MainDevice->getGUIEnvironment();
ITexture *BGTex; 
After this you can add the extern declaration to the other file:

Code: Select all

extern IrrlichtDevice *MainDevice; //The Main Irrlicht Device
extern ISceneManager *Scene; //Scene Manager
extern IVideoDriver *Driver; //Video Driver
extern IGUIEnvironment *GUI; //GUI Environment
extern ITexture *BGTex;