This project is read-only.

How to use SharpGL in a Dialog??

Feb 21, 2013 at 7:51 PM
Edited Feb 21, 2013 at 7:51 PM
I created a WPF window as a dialog:
<Window x:Class="Testing_Environment.src.GUI.BooleanResultWindow"
        Title="BooleanResultWindow" Height="300" Width="300"
        <!-- Accept or Cancel -->
        <sharpGL:OpenGLControl OpenGLDraw="OpenGLControl_OpenGLDraw_1" 
and open this window this way:
           BooleanResultWindow w = new BooleanResultWindow();
            w.Owner = this;
Now the problem is, when i close this dialog the OpenGLDraw method is still running. So whats wrong with my code?
Feb 23, 2013 at 5:01 PM
Without looking at the rest of the structure of the code and project, I would guess that this is due to a fairly common problem in C#, which is the fact that registering event handlers creates a strong reference. Basically, without registering any event handlers, you're OpenGL control would get created, then it would exist for a while, then the dialog would close, the reference count would decrement and the object would be (eventually) garage collected. Now that you have an event handler (in fact three), the reference count is incremented three times - when the window closes, the object is still referenced, via the event handlers, so it doesn't dispose.

This issue could be (possibly) resolved by using weak event handlers, which don't increment the reference count, but actually I think it is essentially a bug in SharpGL - if the control is in a window that is closing, the render loop should terminate anyway. So I will raise a bug and fix this in the next release.
Feb 23, 2013 at 5:02 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 29, 2013 at 3:15 PM
The workaround I found is to remove event handlers on Unloaded event:
         public OpenGLControl()
            SizeChanged += new SizeChangedEventHandler(OpenGLControl_SizeChanged);
            this.Unloaded += new RoutedEventHandler(OpenGLControl_Unloaded);

        void OpenGLControl_Unloaded(object sender, RoutedEventArgs e)
            SizeChanged -= new SizeChangedEventHandler(OpenGLControl_SizeChanged);
            timer.Tick -= new EventHandler(timer_Tick);
            Unloaded -= new RoutedEventHandler(OpenGLControl_Unloaded);
May 31, 2013 at 2:02 PM
I will give it a shot as soon as I find time for it.