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

Post by hybrid »

These definitions cannot be global because global variables cannot be initialized by method calls. So there are probably hundreds of errors due to these statements. Try to avoid global definitions or be really sure what you're doing!
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

First off, it is probably best if you avoid the use of global variables if you can. It appears that the problem you have is that you have declared a global and a local variable with the same name. When you initialize the local variable, it has no effect on the global. At some time later in your program, you access the global, but it was never initialized.

If you wish to continue using globals, you should do this...

Code: Select all

// the header file declares the variables
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;

Code: Select all

// the source file defines the variables
IrrlichtDevice *MainDevice = 0;
ISceneManager *Scene = 0;
IVideoDriver *Driver = 0;
IGUIEnvironment *GUI = 0;
ITexture *BGTex = 0;

Code: Select all

// the initialization routine initializes the variables
void initSystems()
{
  IEventReceiver* eventReceiver = new EventReceiver;
  MainDevice= createDevice(EDT_DIRECT3D9, dimension2d<s32>(800, 600), 16, false, true, false, eventReceiver); 

  Scene = MainDevice->getSceneManager(); 
  Driver = MainDevice->getVideoDriver(); 
  GUI = MainDevice->getGUIEnvironment();
}

void initMenu()
{
  // ...
  BGTex = Driver->getTexture("2D Images/Menu/BG.bmp"); 
  // ...
}
Another thing that you might want to consider doing is adding a prefix to your variable names so you know what scope they are from. Using a g to indicate global and m for member. That will help to avoid some naming issues in the future.

Travis
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

vitek wrote:It appears that the problem you have is that you have declared a global and a local variable with the same name. When you initialize the local variable, it has no effect on the global. At some time later in your program, you access the global, but it was never initialized.
Exactly what I wrote 4 or 5 posts before... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Yeah, I know. Unfortunately most beginners don't understand all of the big words you throw at them. Sometimes easiest way is to show them.

Travis
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post by Xharock »

Don't worry Acki I knew what you meant :)
Thanks for your help. I'm a begginner to bigger projects. I've only ever done small mini things never something on such a big scale. Thanks again. Oh and I am now in the process of removing many of my global variables. My BGTex doesn't need to be global as only one module will be using it.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post by Xharock »

I can't beleive this still don't work... nevermind. I'm scrapping that and starting over. And try and get it all OK. This was poorly designed.
Xharock
Posts: 71
Joined: Wed May 10, 2006 3:50 pm

Post by Xharock »

Yay! I have re-started and re-designed my code layout and all is working nicely. I'm thinking of incorporating some sort of class. But thanks for ally our help!
iRobo
Posts: 26
Joined: Mon Aug 23, 2010 1:41 pm
Location: Iran
Contact:

Re: ITexture is giving Access Violation Error

Post by iRobo »

I still have the same problem. but my reading location is 0x00000001 instead of 0x00000000

Code: Select all

0xC0000005: Access violation reading location 0x00000001
it doesn't matter where I use getTexture command, I get this error anyway!
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: ITexture is giving Access Violation Error

Post by hybrid »

You probably have an out-of-bounds array access somewhere. This overwrites your pointer and leaves the 1 there.
Post Reply