NullReferenceException - Sprite.OnLostDevice

Ok I'm finding this difficult to understand... I'm so used to destructors in C++.

When I exit my application, I am getting a NullReferenceException, stacktrace

   at Microsoft.DirectX.Direct3D.Sprite.OnLostDevice()
   at Microsoft.DirectX.Direct3D.Sprite.OnParentLost(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at Microsoft.DirectX.Direct3D.Device.raise_DeviceLost(Object i1, EventArgs i2)
   at Microsoft.DirectX.Direct3D.Device.Finalize()

Now I have this UI manager class that implements a LinkedList<> of user classes one is a generic control base with some properties and virtual methods and a label class that inherits from that.

All fine and dandy. I intialise a class of type Label and add it to the manager, it draws text just fine using a Direct3D.Sprite and Direct3D.Font.

I have implemented an IDispose interface on the Control class in an attempt to have the controls dispose of their sprites and fonts when the app exits.

I have even implemented Device Lost and Reset events in which I call all of the dispose methods in lost, and recreate the UI entirely in reset.

Nothing has yet stopped the crashing, except commenting out all of the lines to do with sprites. The font still renders fine but doesn't crash.

Obviously, I am doing something wrong, does anyone have any clue where I may be going wrong It's hard to post code because it's spread out and large.

Oh, when run in debug mode and I exit the app, I get the exception with no reference to where it occured in the source code.



Answer this question

NullReferenceException - Sprite.OnLostDevice

  • Alan Simes

    Ok, I made my interface class use a global (declared in the form) sprite and font object, now I can use it all fine and it doesn't crash, provided I do not dispose of the sprite or font during a DeviceLost event, and in the FormClosing method, I dispose of the device and set it to null.

    I'm not really sure why this works, but it does show a basic lack of my understanding of the way in which device resources should be managed by the end programmer.


  • NullReferenceException - Sprite.OnLostDevice