Skip to main content
gamedev.net
🔒 Locked

Help with NullReferenceException Font.OnLostDevice()

Started by Kap 1 reply 850+ views
Original Post
Kap
Kap
Hi Guys, Ive been having a bit of trouble with the above exception when using 2D fonts in Managed DirectX (C#.NET). It occurs when i close (i.e. click X, press esc, this.Close() etc) my window and after reading through some resources and posts on here i think its related to my pants resource disposing :) Ive had a go at trying to solve this but i dont think i totally grasp the concepts enough to know where to place my method calls properly. I've outlined my code below and explained how its called etc. If someone could very kindly point me in the right direction of where im going wrong it would be absolutely grand :D My app entry is a AppEntry class which has a static void Main() which calls:
RenegadeWindow renegadeWindow = new RenegadeWindow();
Application.Idle += new EventHandler(renegadeWindow.OnApplicationIdle);
Application.Run(renegadeWindow);


renegadeWindow is my render target window and is just a win form. The game loop is controlled via the PeekMessage method from Tom Miller's blog using the ApplicationIdle etc and that basically calls a method in a seperate class "InitEngine". This basically just has a list of commands to say, render the FPS on screen, render some sprites, start stop drawing etc such as here:

        public InitEngine(Form form)
        {
            core = new Core(form);

            device = core.Device;

            indexBuff = new IndexBuff(core);
            frameRate = new FrameRate(core);
        }

        public void Render()
        {
            try
            {
                core.BeginRender();
                    frameRate.RenderText();
                    indexBuff.Render();
                core.EndRender();                
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occured: " + ex.Message + "\n\n" + ex.StackTrace);
            }



All it does it has a Form object passed into the InitEngine constructor which grabs the device etc. My particular scene to render is indexBuff (a quick test im playing with to try out index buffers), so it just draws that and my fps counter inbetween some BeginRender() and EndRender() methods. Now i have implemented in my IndexBuff class an overriden Dispose() method similar to one Peter Germishuys used in a similar thread on here. My question is how do i call it? Would i place it before the core.BeginRender() in a disposing check? Ive tried playing around with it but i either get an error upon the form loading (object ref not set, as presumably im destroying the object before i can use it) or i get the NullRefError when i exit the form. Any help or direction that can be given would be greatly appreciated. It's been driving me insane! [bawling] If you need more clarification on how my (admittedly simple, yet pants) engine works, please let me know. I never was any good at explaining things :(
Kap
Kap
Thought id add a bit more info about my render loop to clarify exactly how things work.

The game loop is controlled by an implementation of Tom Miller's method of using OnApplicationIdle.

The "application" starts with a class "AppEntry" (not in the form code itself) which has my static void Main() which does:

RenegadeWindow renegadeWindow = new RenegadeWindow();
Application.Idle += new EventHandler(renegadeWindow.OnApplicationIdle);
Application.Run(renegadeWindow);

My OnApplicationIdle is located within the Forms code body and basically calls a method "initEngine.Render()". The InitEngine class basically just allows me to piece together all my scene drawing, input management etc in 1 drawing method - Render().

Different classes (say you had a class that drew a simple triangle) would inherit the DX Device from the Core class which just sets up everything in terms of devices, presentparams.

My question really is, how do i implement proper GC around this as the normal "Dispose()" doesnt seem to pick everything up. I think the issue is that the form disposes itself before the DX device and fonts and what not, and tries to execute a final Render() with nothing to render to (hence object ref not set).

Ive tried manually implementing dispose methods all over the place but i just cant seem to get it working properly. If anyone could provide some help it really would be appreciated!
Kap
Kap
Nevermind - fixed it! I just removed my "InitEngine" class and added the Render() method into the code for my Form. Can now access the dispose methods for my resource classes within my main Dispose() method.

If anyone wants to suggest a more elegant way please feel free to do so however :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.