Skip to content

Commit

Permalink
Fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
justinstenning committed Feb 3, 2014
1 parent bfcfbc0 commit 8eeb671
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
46 changes: 45 additions & 1 deletion Capture/Interface/Screenshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
using System.Text;
using System.Drawing;
using System.IO;
using System.Runtime.Remoting;
using System.Security.Permissions;

namespace Capture.Interface
{
public class Screenshot : MarshalByRefObject
public class Screenshot : MarshalByRefObject, IDisposable
{
private bool _disposed;

public Screenshot(Guid requestId, byte[] capturedBitmap)
{
_requestId = requestId;
_capturedBitmap = capturedBitmap;
}

~Screenshot()
{
Dispose(false);
}

Guid _requestId;
public Guid RequestId
{
Expand All @@ -32,6 +41,41 @@ public byte[] CapturedBitmap
return _capturedBitmap;
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
Disconnect();
}
_disposed = true;
}
}

/// <summary>
/// Disconnects the remoting channel(s) of this object and all nested objects.
/// </summary>
private void Disconnect()
{
RemotingServices.Disconnect(this);
}

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService()
{
// Returning null designates an infinite non-expiring lease.
// We must therefore ensure that RemotingServices.Disconnect() is called when
// it's no longer needed otherwise there will be a memory leak.
return null;
}
}

public static class BitmapExtension
Expand Down
2 changes: 1 addition & 1 deletion TestScreenshot/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void DoRequest()
/// <param name="screenshotResponse"></param>
void Callback(IAsyncResult result)
{
Screenshot screenshot = _captureProcess.CaptureInterface.EndGetScreenshot(result);
using (Screenshot screenshot = _captureProcess.CaptureInterface.EndGetScreenshot(result))
try
{
_captureProcess.CaptureInterface.DisplayInGameText("Screenshot captured...");
Expand Down

0 comments on commit 8eeb671

Please sign in to comment.