ITexture is giving Access Violation Error

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

ITexture is giving Access Violation Error

Post 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?
TheGameMaker
Posts: 275
Joined: Fri May 12, 2006 6:37 pm
Location: Germany

Post 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....
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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?
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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;
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: ITexture is giving Access Violation Error

Post 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 !!!
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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)...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post 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
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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;
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply