1

Closed

Spritebatch.Draw exception

description

Hi.

I have the following problem with SpriteBatch.Draw():

Unhandled exception at 0x01024A00 in ZXpp.exe: 0xC0000005: Access violation reading location 0x00000000.

I setup the spritebatch with spriteBatch.reset( new SpriteBatch( dContext) );

And .png textures loaded with CreateWICTextureFromFile(...

Sometimes everything just works, but 90%+ of the times it fails with this error - I guess I'm doing something wrong...)

Cheers

James
Closed May 30, 2013 at 9:43 PM by walbourn

comments

ShawnHargreaves wrote Apr 12, 2013 at 7:53 PM

It is impossible to diagnose your problem with so little information.

What is the callstack when it crashes?

What is your code doing? Please provide a repro app.

johara wrote Apr 12, 2013 at 9:50 PM

Sorry, you are quite right.

My code is based on the sample app downloaded with the toolkit.

Here is how I load my textures (in a constructor for a class called TextureCache):

TextureCache::TextureCache(ID3D11Device* device, ID3D11DeviceContext* context)
{
//  load all textures here
block = 0;
string blockName = "pixelSlices\\block.png";
wstring wName = wstring(blockName.begin(), blockName.end());
CreateWICTextureFromFile( device, context, wName.c_str(), nullptr, &block);

for (int i = 0; i < 256; i++)
{
    string name = "pixelSlices\\pixelSlice";
    name += to_string(i);
    name += ".png";
    wstring wideName = wstring(name.begin(), name.end());
    g_pTextureRV1[i] = 0;
    CreateWICTextureFromFile( device, context, wideName.c_str(), nullptr, &g_pTextureRV1[i] );
}
}

With extraneous code removed i then render like so:

spriteBatch->Begin(SpriteSortMode_Deferred);
spriteBatch->Draw(cache->getBlock(), XMFLOAT2(startX, (borderHeight + (line * 8)) * screenScale), NULL, colors[paper], 0.f, XMFLOAT2(0, 0), screenScale);
spriteBatch->End();

My stack trace is:
ZXpp.exe!ULA::render() Line 248 C++
ZXpp.exe!Render() Line 301  C++
ZXpp.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 78  C++
ZXpp.exe!__tmainCRTStartup() Line 528   C
kernel32.dll!770233aa() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
ntdll.dll!77829ef2()    Unknown
ntdll.dll!77829ec5()    Unknown
Interestingly, this bug only happens in release mode. If I change to debug mode, everything works.

Cheers

James

walbourn wrote Apr 12, 2013 at 10:34 PM

What architecture are you using (x86, ARM, x64?) and which compiler exactly? Have you tried installing VS 2012 Update 2?

ShawnHargreaves wrote Apr 12, 2013 at 11:10 PM

ZXpp.exe!ULA::render() Line 248 C++
And what is this line of code?

johara wrote Apr 13, 2013 at 11:48 AM

Hi,
Am using x64, standard Visual Studio 2012 compiler - update 1 only - will now d/l update 2.

ZXpp.exe!ULA::render() Line 248 C++

The above class (ULA) is where the spritebatch lives. render() just calls the spritebatch.draw() stuff...

Cheers

James

johara wrote Apr 13, 2013 at 12:12 PM

Hi, just to confirm I've now updated to Update 2. Although am x64 my build platform is Win32. Still same problems, everything OK with debug build, exception with release build.

Cheers

James

ShawnHargreaves wrote Apr 13, 2013 at 7:29 PM

ZXpp.exe!ULA::render() Line 248 C++

The above class (ULA) is where the spritebatch lives. render() just calls the spritebatch.draw() stuff...
Sorry, but my telepathic powers are not up to the job of helping you given so little info. You are going to have to provide FAR more detail before anyone else can guess what you are doing wrong.

Starting with, what is at the line of code where your program is crashing? This callstack points to your own code, not inside SpriteBatch. There is a chance the compiler could be inlining some SpriteBatch code into this method body, but more likely it's an error before it even gets as far as calling SpriteBatch.

Without knowing what that line of code is (or even better, seeing a proper repro app) there is nothing I can do to help you.

johara wrote Apr 13, 2013 at 8:39 PM

OK, sorry, I'm just not sure what is helpful and what is noise.
what is at the line of code where your program is crashing?
spriteBatch->Draw(cache->getBlock(), XMFLOAT2(startX, (borderHeight + (line * 8)) * screenScale), NULL, colors[paper], 0.f, XMFLOAT2(0, 0), screenScale);

getBlock() above calls this method:

ID3D11ShaderResourceView* TextureCache::getBlock()
{
return block;
}

the ID3D11ShaderResourceView* is created by:
block = 0;
string blockName = "pixelSlices\\block.png";
wstring wName = wstring(blockName.begin(), blockName.end());
CreateWICTextureFromFile( device, context, wName.c_str(), nullptr, &block);


(or even better, seeing a proper repro app)
My full application (a ZX Spectrum emulator) is available from github. the file ula.cpp contains all the spritebatch code, the application setup & loop is in Application.cpp (mostly copied from the toolkit example). The only other file that might be involved as far as i can see is TextureCache.cpp where the textures are initially loaded as above.

See https://github.com/jo215/ZXpp for the full code.

I appreciate it,

James

ShawnHargreaves wrote Apr 13, 2013 at 10:31 PM

You're crashing due to a null pointer read, so you need to look in the debugger to see what data it is trying access that is null. Perhaps 'cache' isn't set? Is 'paper' null? Or 'spriteBatch' itself null?

I don't have time to debug through your whole big program, sorry. A repro app means to put together a small-as-possible standalone app that demonstrates the problem you are having, separated out from the rest of the program. This makes it easier for others to understand, and also has the advantage that a lot of times you will figure out the mistake yourself during the process of putting together the repro app...

johara wrote Apr 13, 2013 at 11:33 PM

I understand. Not enough and then too much information.

However, I have tracked down the source of the crash, and its an odd one. The following line at the start of my DirectSound setup method is the cause:

DirectSoundCreate8(NULL, &directSound, NULL);

If I omit this (and obviously the rest of the audio setup) then its goodbye to my crashes.

Cheers

James

walbourn wrote Apr 14, 2013 at 6:00 AM

If you have a memory leak, uninitialized pointer, or similar problem (as is often indicated by 'Debug' working but 'Release' crashing) then changing the problem significantly can often cause it to seem to go away but it's still there. Make sure you are compiling with Warning Level 4 and fix all warnings. You might even want to go so far as to run Code Analysis on it and check out all messages there too.

Also, DirectSound is deprecated. You should consider XAUDIO2 or Media Foundation for your audio needs.