Skip to content

Commit afba7a4

Browse files
committed
Add PinBox.cs
1 parent 98966e6 commit afba7a4

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/Imageflow/Bindings/PinBox.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.ConstrainedExecution;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
7+
namespace Imageflow.Net.Bindings
8+
{
9+
internal class PinBox : CriticalFinalizerObject, IDisposable
10+
{
11+
private List<GCHandle> _pinned;
12+
internal void AddPinnedData(GCHandle handle)
13+
{
14+
if (_pinned == null) _pinned = new List<GCHandle>();
15+
_pinned.Add(handle);
16+
}
17+
18+
public void Dispose()
19+
{
20+
UnpinAll();
21+
GC.SuppressFinalize(this);
22+
}
23+
24+
private void UnpinAll()
25+
{
26+
//Unpin GCHandles
27+
if (_pinned != null)
28+
{
29+
foreach (var active in _pinned)
30+
{
31+
if (active.IsAllocated) active.Free();
32+
}
33+
_pinned = null;
34+
}
35+
}
36+
37+
~PinBox()
38+
{
39+
UnpinAll();
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)