Jump to content
  • Advertisement
Sign in to follow this  

[C#] [XNA] Access Violation Exception with textures.

This topic is 3087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to create my texture list before creating my components. This way I can have just one place in which the textures are stored and accessed using texture indices.

Problem is, if I create a list of all possible textures during the Game constructor:

public Game1()
graphics_Device_Manager = new GraphicsDeviceManager(this);
graphics_Device_Manager.PreferredBackBufferWidth = 1280;
graphics_Device_Manager.PreferredBackBufferHeight = 720;
graphics_Device_Manager.IsFullScreen = false;
graphics_Device_Manager.SynchronizeWithVerticalRetrace = false; // For testing purposes (can see how high FPS are from one method compared to another)
IsFixedTimeStep = false; // Also needed for not limiting FPS
Content.RootDirectory = "Content";

// Load Textures
Texture2D arrow_64x64 = Content.Load<Texture2D>("Textures\\Arrow_64x64");
arrow_64x64.Name = "Arrow_64x64";

Texture2D blank_1x1 = Content.Load<Texture2D>("Textures\\Blank_1x1");
blank_1x1.Name = "Blank_1x1";

Texture2D cursor_32x32 = Content.Load<Texture2D>("Textures\\Cursor_32x32");
cursor_32x32.Name = "Cursor_32x32";

Texture2D debug_Lighting_1024x1024 = Content.Load<Texture2D>("Textures\\Debug_Lighting_1024x1024");
debug_Lighting_1024x1024.Name = "Lighting_1024x1024";

Texture2D megaMan_160x32 = Content.Load<Texture2D>("Textures\\MegaMan_160x32");
megaMan_160x32.Name = "MegaMan_160x32";

Texture2D pixel_Texel_Align_64x64 = Content.Load<Texture2D>("Textures\\_Pixel_Texel_Align_64x64");
pixel_Texel_Align_64x64.Name = "Pixel_Texel_Align_64x64";

Texture2D wall_16x16 = Content.Load<Texture2D>("Textures\\Wall_16x16");
wall_16x16.Name = "Wall_16x16";

then I will get the following error when trying to set textures in effects:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

This line causes the problem above:

effect.Parameters["Texture0"].SetValue(game.Textures[texture]); // uses texture index

How can this be avoided and what is causing the error?

Share this post

Link to post
Share on other sites
Maybe it's a problem with the fact that you need a valid device to create valid texture objects. If you call the Content.Load function from your constructor a valid device does not exist there.

From MSDN:

"The GraphicsDeviceManager initializes the GraphicsDevice before you call Game.Initialize."

"Game.Initialize Method
Called after the Game and GraphicsDevice are created, but before LoadContent."

From the above I conclude that the Graphics Device is only valid after the call to the Game's constructor so you are loading textures with an invalid device.

Share this post

Link to post
Share on other sites
I agree, you should not be loading any content before the Game.LoadContent method. Try moving your texture loading in there and see if that works.

If it turns out that you are trying to access the global list before the game has loaded the textures (i.e. in game component constructors, etc) you will need to change those to wait until the component LoadContent is called as well.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!