Skip to content

Commit

Permalink
Merge pull request #74 from robinsedlaczek/MemoryLeakInWpfControl
Browse files Browse the repository at this point in the history
Memory leak in wpf control
  • Loading branch information
dwmkerr committed Jan 28, 2015
2 parents 2df2cda + 20ec72e commit f90a381
Showing 1 changed file with 53 additions and 23 deletions.
76 changes: 53 additions & 23 deletions source/SharpGL/Core/SharpGL.WPF/OpenGLControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;
using System.Windows.Interop;
using System.Diagnostics;
using SharpGL.RenderContextProviders;
using SharpGL.SceneGraph;
using SharpGL.Version;

Expand All @@ -31,21 +23,63 @@ public partial class OpenGLControl : UserControl
public OpenGLControl()
{
InitializeComponent();
SizeChanged += new SizeChangedEventHandler(OpenGLControl_SizeChanged);

timer = new DispatcherTimer();

Unloaded += OpenGLControl_Unloaded;
Loaded += OpenGLControl_Loaded;
}

/// <summary>
/// Handles the Loaded event of the OpenGLControl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> Instance containing the event data.</param>
private void OpenGLControl_Loaded(object sender, RoutedEventArgs routedEventArgs)
{
SizeChanged += OpenGLControl_SizeChanged;

UpdateOpenGLControl((int) RenderSize.Width, (int) RenderSize.Height);

// DispatcherTimer setup
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}

/// <summary>
/// Handles the Unloaded event of the OpenGLControl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> Instance containing the event data.</param>
private void OpenGLControl_Unloaded(object sender, RoutedEventArgs routedEventArgs)
{
SizeChanged -= OpenGLControl_SizeChanged;

timer.Stop();
timer.Tick -= timer_Tick;
}

/// <summary>
/// Handles the SizeChanged event of the OpenGLControl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.SizeChangedEventArgs"/> instance containing the event data.</param>
/// <param name="e">The <see cref="System.Windows.SizeChangedEventArgs"/> Instance containing the event data.</param>
void OpenGLControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
// Lock on OpenGL.
UpdateOpenGLControl((int) e.NewSize.Width, (int) e.NewSize.Height);
}

/// <summary>
/// This method is used to set the dimensions and the viewport of the opengl control.
/// </summary>
/// <param name="width">The width of the OpenGL drawing area.</param>
/// <param name="height">The height of the OpenGL drawing area.</param>
private void UpdateOpenGLControl(int width, int height)
{
SizeChangedEventArgs e;
// Lock on OpenGL.
lock (gl)
{
int width = (int)e.NewSize.Width;
int height = (int)e.NewSize.Height;
gl.SetDimensions(width, height);

// Set the viewport.
Expand All @@ -64,7 +98,7 @@ void OpenGLControl_SizeChanged(object sender, SizeChangedEventArgs e)
gl.LoadIdentity();

// Calculate The Aspect Ratio Of The Window
gl.Perspective(45.0f, (float)width / (float)height, 0.1f, 100.0f);
gl.Perspective(45.0f, (float) width/(float) height, 0.1f, 100.0f);

gl.MatrixMode(OpenGL.GL_MODELVIEW);
gl.LoadIdentity();
Expand Down Expand Up @@ -105,11 +139,7 @@ public override void OnApplyTemplate()
if (handler != null)
handler(this, eventArgsFast);

// DispatcherTimer setup
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 0, 0, (int)(1000.0 / FrameRate));
timer.Start();
}

/// <summary>
Expand Down Expand Up @@ -149,7 +179,7 @@ void timer_Tick(object sender, EventArgs e)
{
case RenderContextType.DIBSection:
{
var provider = gl.RenderContextProvider as RenderContextProviders.DIBSectionRenderContextProvider;
var provider = gl.RenderContextProvider as DIBSectionRenderContextProvider;
var hBitmap = provider.DIBSection.HBitmap;

if (hBitmap != IntPtr.Zero)
Expand All @@ -167,7 +197,7 @@ void timer_Tick(object sender, EventArgs e)
break;
case RenderContextType.FBO:
{
var provider = gl.RenderContextProvider as RenderContextProviders.FBORenderContextProvider;
var provider = gl.RenderContextProvider as FBORenderContextProvider;
var hBitmap = provider.InternalDIBSection.HBitmap;

if (hBitmap != IntPtr.Zero)
Expand Down

0 comments on commit f90a381

Please sign in to comment.