SpriteBatch.End() giving errors due to invalid parameters?

Nov 5, 2013 at 10:37 AM
Hi there,

I hope this isn't something stupid that I've missed or did wrong, but I am stumped at this point and thought perhaps someone else using the TK had found something similar. The problem I am running into is that everything works 100% fine until I call SpriteBatch.End and at that point I get an error saying "Unhandled exception at <address> (msvdr110d.dll) in <myexe>. An invalid parameter was passed to a function that considers invalid parameters fatal." This is actually inside a C# application using a SurfaceImageSource written in C++ to use DirectX, I have managed successfully draw using Direct2D (I only tried primitives, like lines and rectangles) but would rather use the Toolkit for SpriteBatch and having the DDSTextureFromFile functions. The app targets W8 Store, I have the DirectX stuff inside a WinRT component that I am writing.

I have a BeginDraw function that allows me to draw to sections of my underlying surface that looks like the following:
void MyImageSourceType::BeginDraw(Windows::Foundation::Rect updateRect)
    POINT offset;
    ComPtr<IDXGISurface> surface;
    // Express target area as a native RECT type.
    RECT updateRectNative;
    updateRectNative.left = static_cast<LONG>(updateRect.Left);
    updateRectNative.top = static_cast<LONG>(updateRect.Top);
    updateRectNative.right = static_cast<LONG>(updateRect.Right);
    updateRectNative.bottom = static_cast<LONG>(updateRect.Bottom);
    // Begin drawing - returns a target surface and an offset
    // to use as the top-left origin when drawing.
    HRESULT beginDrawHR = m_sisNative->BeginDraw(
      updateRectNative, &surface, &offset);
    if (beginDrawHR == DXGI_ERROR_DEVICE_REMOVED ||
      // If the device has been removed or reset, attempt to
      // re-create it and continue drawing.

    ComPtr<ID3D11Texture2D> texture;

    m_d3dDevice->CreateRenderTargetView(texture.Get(), nullptr, &m_d3dRenderTargetView);

    auto context = m_d3dDeviceContext;
    auto renderTarget = m_d3dRenderTargetView;
    auto depthStencil = m_d3dDepthStencilView.Get();

    context->ClearRenderTargetView(renderTarget.Get(), Colors::White);
    //context->ClearDepthStencilView(depthStencil, D3D11_CLEAR_DEPTH, 1.0F, 0);
    context->OMSetRenderTargets(1, &renderTarget, depthStencil);
And my simple draw code and EndDraw look like the following:
void MyImageSourceType::EndDraw()

void MyImageSourceType::DrawPoint(int x, int y)

    m_spriteBatch->Draw(m_texNote1.Get(), XMFLOAT2(x,y));
I have followed the example from http://msdn.microsoft.com/en-us/magazine/jj991975.aspx to get this all set up drawing to an image.

My setting up of the spritebatch is at the end of my CreateDeviceResources() function and is simply the following:
      m_spriteBatch.reset(new SpriteBatch(m_d3dDeviceContext.Get()));

      CreateDDSTextureFromFile(m_d3dDevice.Get(), L"assets\\windowslogo.dds", nullptr, m_texNote1.ReleaseAndGetAddressOf());
I stepped through and it seems like everything is setting up and loading correctly, I know I don't have HRESULT checks in here but I went through 1-by-1 and made sure everything was giving S_OK before posting (and it was).

If you can explain to me how to set up debugging to also step through the toolkit code I can find more specifics for where things are going wrong?

DirectXTK version: (installed via nuget)
VS2012, Im running Win8.1 but targeting Win8 with the project.

Any other information that can help you I am happy to add in here.

Thanks in advance for you help!
Nov 5, 2013 at 4:23 PM
Did you try enabling the Direct3D 11 Debug Layer and look for any output? Note since you are using Windows 8.1, you need to make sure you install the updated debug layer. See Direct3D SDK Debug Layer Tricks and Visual Studio 2013 and Windows 8.1 SDK RTM are now available. This is always the first thing to try when debugging Direct3D.

Note that the NuGet DirectXTK version is a bit old (it's back from early in the year). There's a new one being published soon which is sync'd to the October 2013 release.
Nov 6, 2013 at 8:58 AM
Edited Nov 6, 2013 at 9:11 AM
I had it enabled, the only message I get is this as it crashes:
First-chance exception at 0x75FF2EEC in SheetMusicAppTemplate.exe: Microsoft C++ exception: std::exception at memory location 0x056EDB8C.
I can post the full log if you need. If I step over line by line I get to the SpriteBatch->End call() at the end of my DrawPoint function (I use F10 from the start of my code) and then goes into a view saying "Source code is unavailable for this", another step gives the same thing, and the third step throws the exception above which forces the Debugger to break and I cannot continue.

Minor Edit: I can always wait for the updated Nuget package and see if it works when that comes through.
Nov 7, 2013 at 6:49 AM
OK, so "std::exception" looks like an error was thrown by the DirectXTK. Have you enabled "Break on C++ Exceptions"?
Nov 7, 2013 at 12:46 PM
I have enabled Break on C++ Exceptions, the error occurs immediately after getting the pointer to the SpriteBatch instance.
Nov 7, 2013 at 7:35 PM
Do you have a line of code reference for that break?
Nov 8, 2013 at 10:04 AM
This is the trace while stepping into everything:

-> Include/memory (line 1408), start brace of: pointer operator->() const _NOEXCEPT
|--> Moves onto line 1409: return (_STD pointer_traits<pointer>::pointer_to(**this));
|--> Moves onto start brace (line 1403), of: typename add_reference<_Ty>::type operator*() const
|--> Moves onto line 1404: return (*this->_Myptr);
|--> Returns value
-> Include/xmemory0 (line 232): start brace of: static pointer pointer_to(_Reftype _Val)
|--> Moves onto line 233: return (_STD addressof(_Val));
|---> Include/xstddef (line 85), start brace of function: template<class _Ty> inline _Ty *addressof(_Ty& _Val) _NOEXCEPT
|---> Moves onto line 86: return (reinterpret_cast<_Ty *>((&const_cast<char&>(reinterpret_cast<const volatile char&>(_Val)))));
|---> Returns value
-> Returns value, shows the m_spriteBatch->End() function line I started from.
[Unhandled exception at 0x086973B3 (msvcr110d.dll) in SheetMusicAppTemplate.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.]

To me that looks like it is all memory management for the pointer to the spriteBatch itself. Would it perhaps be more convenient for my to put the code on GitHub or something like that temporarily for you to take a look at?