slugonamission

I have a really weird and annoying error appearing.

Basically, I'm following the Hazy Mind engine tutorials for XNA, and I can't get post processing working correctly.

I get this error occouring

An unhandled exception of type 'System.AccessViolationException' occurred in Microsoft.Xna.Framework.dll

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

This does not always happen in the same place though, if I turn off code optimisation, it happens in the line above. I have run Windows Memory Diagnostic and my memory seems fine.

Here is the code

public void ResolveAndRender(GraphicsDevice myDevice)
{
myDevice.ResolveRenderTarget(0);
myDevice.SetRenderTarget(0, null);
myDevice.Clear(Color.CornflowerBlue);

myEffect.Begin();

myEffect.Parameters["screenTex"].SetValue(myRenderTarget.GetTexture());

foreach (EffectPass pass in myEffect.CurrentTechnique.Passes)
{
pass.Begin();

myDevice.VertexDeclaration = vDecl;
myDevice.Vertices[0].SetSource(vb, 0, bytesPerVertex);
myDevice.Indices = ib;
myDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, 0, numPrimitives);
pass.End();
}

myEffect.End();
}


It will either break on pass.Begin() with optimisation enabled, or myDevice.VertexDeclaration... with it disabled.

Also, if I change my shader compile version from 2.0 to 3.0, it throws an nullreferenceexcpetion for myEffect.CurrentTechnique.Passes. (And my card does support SM3).

Lastly, it also seems to depend on what is in the scene. For example, if I add a mesh to the scene, with SM2, it will compile and run, but I just get a white screen.

I really can't find what is the problem here, nor do I know an easy way of reproducing the problem. Does anybody else have any ideas

Thanks in advance ~ Jamie


Re: XNA Framework AccessViolationExcpetion

Rim van Wersch

Looks a lot like my problem I reported on MS Connect. If it is the same problem, you're probably disposing the effect object in code outside the content manager. The content manager fails to see the effect object is disposed, so it doesn't reload it and you end up calling into the wrong memory when working with the effect.

So basically, just don't call Dispose() on an effect object created by the content manager






Re: XNA Framework AccessViolationExcpetion

slugonamission

I'm not calling dispose on anything anywhere though :(

The only other thing could be I was setting it to null instead of comparing it to null, but wouldn't that throw a NullReferenceException

I may try the manual compile method, see if that works :(




Re: XNA Framework AccessViolationExcpetion

Jim Perry

I get this error when I try to restart a game after an unhandled exception. Restarting GSE usually fixes the problem. It might not be the same kind of problem with you, but you might want to give it a shot.




Re: XNA Framework AccessViolationExcpetion

slugonamission

I've kinda sorted it.

Compiling the shader manually from within the program seems to work.

It's a bit rubbish code wise because it hard-codes the shader path, but it works :)

CompiledEffect compiledEffect = Effect.CompileEffectFromFile(@"C:\Shaders\PostProc.fx", null, null, CompilerOptions.None, TargetPlatform.Windows);
myEffect = new Effect(myGraphics, compiledEffect.GetEffectCode(), CompilerOptions.None, null);




Re: XNA Framework AccessViolationExcpetion

Shawn Hargreaves - MSFT

slugonamission wrote:
I'm not calling dispose on anything anywhere though :(

The only other thing could be I was setting it to null instead of comparing it to null, but wouldn't that throw a NullReferenceException

I may try the manual compile method, see if that works :(


If you ever dispose your ContentManager, or call Unload on it, or stop storing a reference to it so it gets garbage collected, that will also cause the effect to be disposed.





Re: XNA Framework AccessViolationExcpetion

Stephen Styrchak - MSFT

Jim Perry wrote:
I get this error when I try to restart a game after an unhandled exception. Restarting GSE usually fixes the problem. It might not be the same kind of problem with you, but you might want to give it a shot.

I believe this is an unrelated issue.

There is a known bug in the Exception Assistant (aka, Debugging Helper or Exception Helper) that causes memory corruption in XNA Game projects. The bug will be fixed with the release of VS 2005 SP1 for Express. Until then, you should disable the Exception Assistant.

To disable the Exception Assistant:

  1. Go to Tools | Options.
  2. Select the Debugging node.
  3. Uncheck "Enable the exception assistant".

If the Exception Assistant has already been displayed during a session, close and reopen the IDE (before or after the steps above).

For Xbox 360 projects, the Exception Assistant is already off (not supported with the Xbox 360 remote debugger), but it's possible to have other projects in the same solution that may activate it, so it's best to turn it off until VS 2005 SP1 is available. You may also try the VS 2005 SP1 Beta.

Regards,

--Stephen





Re: XNA Framework AccessViolationExcpetion

slugonamission

I can't see anywhere in my code I am doing that, so I don't think it is that causing the problem.

Is the compiler for the content pipeline the same as that by calling Effect.CompileEffectFromFile I really can't see where this error is coming from




Re: XNA Framework AccessViolationExcpetion

slugonamission

After a bit of playing around, the two shaders are slightly different (the compiled at run time one, and the compiled by the content pipeline one, but only very slightly. They are both being compiled by the same version of D3DX9 shader compiler (9.15.779.0000 in my case).

Problem is, I don't know where to go from here :(

I can post the two compiled shaders if it would help at all